| ... | @@ -78,8 +78,8 @@ const WValue = union(enum) { | ... | @@ -78,8 +78,8 @@ const WValue = union(enum) { |
| 78 | /// bottom of the stack. For instances where `WValue` is not `stack_value` | 78 | /// bottom of the stack. For instances where `WValue` is not `stack_value` |
| 79 | /// this will return 0, which allows us to simply call this function for all | 79 | /// this will return 0, which allows us to simply call this function for all |
| 80 | /// loads and stores without requiring checks everywhere. | 80 | /// loads and stores without requiring checks everywhere. |
| 81 | fn offset(self: WValue) u32 { | 81 | fn offset(value: WValue) u32 { |
| 82 | switch (self) { | 82 | switch (value) { |
| 83 | .stack_offset => |stack_offset| return stack_offset.value, | 83 | .stack_offset => |stack_offset| return stack_offset.value, |
| 84 | else => return 0, | 84 | else => return 0, |
| 85 | } | 85 | } |
| ... | @@ -88,7 +88,7 @@ const WValue = union(enum) { | ... | @@ -88,7 +88,7 @@ const WValue = union(enum) { |
| 88 | /// Promotes a `WValue` to a local when given value is on top of the stack. | 88 | /// Promotes a `WValue` to a local when given value is on top of the stack. |
| 89 | /// When encountering a `local` or `stack_offset` this is essentially a no-op. | 89 | /// When encountering a `local` or `stack_offset` this is essentially a no-op. |
| 90 | /// All other tags are illegal. | 90 | /// All other tags are illegal. |
| 91 | fn toLocal(value: WValue, gen: *Self, ty: Type) InnerError!WValue { | 91 | fn toLocal(value: WValue, gen: *CodeGen, ty: Type) InnerError!WValue { |
| 92 | switch (value) { | 92 | switch (value) { |
| 93 | .stack => { | 93 | .stack => { |
| 94 | const new_local = try gen.allocLocal(ty); | 94 | const new_local = try gen.allocLocal(ty); |
| ... | @@ -103,7 +103,7 @@ const WValue = union(enum) { | ... | @@ -103,7 +103,7 @@ const WValue = union(enum) { |
| 103 | /// Marks a local as no longer being referenced and essentially allows | 103 | /// Marks a local as no longer being referenced and essentially allows |
| 104 | /// us to re-use it somewhere else within the function. | 104 | /// us to re-use it somewhere else within the function. |
| 105 | /// The valtype of the local is deducted by using the index of the given `WValue`. | 105 | /// The valtype of the local is deducted by using the index of the given `WValue`. |
| 106 | fn free(value: *WValue, gen: *Self) void { | 106 | fn free(value: *WValue, gen: *CodeGen) void { |
| 107 | if (value.* != .local) return; | 107 | if (value.* != .local) return; |
| 108 | const local_value = value.local.value; | 108 | const local_value = value.local.value; |
| 109 | const reserved = gen.args.len + @boolToInt(gen.return_value != .none); | 109 | const reserved = gen.args.len + @boolToInt(gen.return_value != .none); |
| ... | @@ -584,7 +584,7 @@ pub const Result = union(enum) { | ... | @@ -584,7 +584,7 @@ pub const Result = union(enum) { |
| 584 | /// Hashmap to store generated `WValue` for each `Air.Inst.Ref` | 584 | /// Hashmap to store generated `WValue` for each `Air.Inst.Ref` |
| 585 | pub const ValueTable = std.AutoArrayHashMapUnmanaged(Air.Inst.Ref, WValue); | 585 | pub const ValueTable = std.AutoArrayHashMapUnmanaged(Air.Inst.Ref, WValue); |
| 586 | | 586 | |
| 587 | const Self = @This(); | 587 | const CodeGen = @This(); |
| 588 | | 588 | |
| 589 | /// Reference to the function declaration the code | 589 | /// Reference to the function declaration the code |
| 590 | /// section belongs to | 590 | /// section belongs to |
| ... | @@ -686,37 +686,34 @@ const InnerError = error{ | ... | @@ -686,37 +686,34 @@ const InnerError = error{ |
| 686 | Overflow, | 686 | Overflow, |
| 687 | }; | 687 | }; |
| 688 | | 688 | |
| 689 | pub fn deinit(self: *Self) void { | 689 | pub fn deinit(func: *CodeGen) void { |
| 690 | for (self.branches.items) |*branch| { | 690 | assert(func.branches.items.len == 0); // we should end with no branches left. Forgot a call to `branches.pop()`? |
| 691 | branch.deinit(self.gpa); | 691 | func.branches.deinit(func.gpa); |
| 692 | } | 692 | func.blocks.deinit(func.gpa); |
| 693 | self.branches.deinit(self.gpa); | 693 | func.locals.deinit(func.gpa); |
| 694 | // self.values.deinit(self.gpa); | 694 | func.mir_instructions.deinit(func.gpa); |
| 695 | self.blocks.deinit(self.gpa); | 695 | func.mir_extra.deinit(func.gpa); |
| 696 | self.locals.deinit(self.gpa); | 696 | func.free_locals_i32.deinit(func.gpa); |
| 697 | self.mir_instructions.deinit(self.gpa); | 697 | func.free_locals_i64.deinit(func.gpa); |
| 698 | self.mir_extra.deinit(self.gpa); | 698 | func.free_locals_f32.deinit(func.gpa); |
| 699 | self.free_locals_i32.deinit(self.gpa); | 699 | func.free_locals_f64.deinit(func.gpa); |
| 700 | self.free_locals_i64.deinit(self.gpa); | 700 | func.* = undefined; |
| 701 | self.free_locals_f32.deinit(self.gpa); | | |
| 702 | self.free_locals_f64.deinit(self.gpa); | | |
| 703 | self.* = undefined; | | |
| 704 | } | 701 | } |
| 705 | | 702 | |
| 706 | /// Sets `err_msg` on `CodeGen` and returns `error.CodegenFail` which is caught in link/Wasm.zig | 703 | /// Sets `err_msg` on `CodeGen` and returns `error.CodegenFail` which is caught in link/Wasm.zig |
| 707 | fn fail(self: *Self, comptime fmt: []const u8, args: anytype) InnerError { | 704 | fn fail(func: *CodeGen, comptime fmt: []const u8, args: anytype) InnerError { |
| 708 | const src = LazySrcLoc.nodeOffset(0); | 705 | const src = LazySrcLoc.nodeOffset(0); |
| 709 | const src_loc = src.toSrcLoc(self.decl); | 706 | const src_loc = src.toSrcLoc(func.decl); |
| 710 | self.err_msg = try Module.ErrorMsg.create(self.gpa, src_loc, fmt, args); | 707 | func.err_msg = try Module.ErrorMsg.create(func.gpa, src_loc, fmt, args); |
| 711 | return error.CodegenFail; | 708 | return error.CodegenFail; |
| 712 | } | 709 | } |
| 713 | | 710 | |
| 714 | /// Resolves the `WValue` for the given instruction `inst` | 711 | /// Resolves the `WValue` for the given instruction `inst` |
| 715 | /// When the given instruction has a `Value`, it returns a constant instead | 712 | /// When the given instruction has a `Value`, it returns a constant instead |
| 716 | fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!WValue { | 713 | fn resolveInst(func: *CodeGen, ref: Air.Inst.Ref) InnerError!WValue { |
| 717 | var branch_index = self.branches.items.len; | 714 | var branch_index = func.branches.items.len; |
| 718 | while (branch_index > 0) : (branch_index -= 1) { | 715 | while (branch_index > 0) : (branch_index -= 1) { |
| 719 | const branch = self.branches.items[branch_index - 1]; | 716 | const branch = func.branches.items[branch_index - 1]; |
| 720 | if (branch.values.get(ref)) |value| { | 717 | if (branch.values.get(ref)) |value| { |
| 721 | return value; | 718 | return value; |
| 722 | } | 719 | } |
| ... | @@ -726,11 +723,11 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!WValue { | ... | @@ -726,11 +723,11 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!WValue { |
| 726 | // means we must generate it from a constant. | 723 | // means we must generate it from a constant. |
| 727 | // We always store constants in the most outer branch as they must never | 724 | // We always store constants in the most outer branch as they must never |
| 728 | // be removed. The most outer branch is always at index 0. | 725 | // be removed. The most outer branch is always at index 0. |
| 729 | const gop = try self.branches.items[0].values.getOrPut(self.gpa, ref); | 726 | const gop = try func.branches.items[0].values.getOrPut(func.gpa, ref); |
| 730 | assert(!gop.found_existing); | 727 | assert(!gop.found_existing); |
| 731 | | 728 | |
| 732 | const val = self.air.value(ref).?; | 729 | const val = func.air.value(ref).?; |
| 733 | const ty = self.air.typeOf(ref); | 730 | const ty = func.air.typeOf(ref); |
| 734 | if (!ty.hasRuntimeBitsIgnoreComptime() and !ty.isInt() and !ty.isError()) { | 731 | if (!ty.hasRuntimeBitsIgnoreComptime() and !ty.isInt() and !ty.isError()) { |
| 735 | gop.value_ptr.* = WValue{ .none = {} }; | 732 | gop.value_ptr.* = WValue{ .none = {} }; |
| 736 | return gop.value_ptr.*; | 733 | return gop.value_ptr.*; |
| ... | @@ -742,34 +739,34 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!WValue { | ... | @@ -742,34 +739,34 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!WValue { |
| 742 | // | 739 | // |
| 743 | // In the other cases, we will simply lower the constant to a value that fits | 740 | // In the other cases, we will simply lower the constant to a value that fits |
| 744 | // into a single local (such as a pointer, integer, bool, etc). | 741 | // into a single local (such as a pointer, integer, bool, etc). |
| 745 | const result = if (isByRef(ty, self.target)) blk: { | 742 | const result = if (isByRef(ty, func.target)) blk: { |
| 746 | const sym_index = try self.bin_file.lowerUnnamedConst(.{ .ty = ty, .val = val }, self.decl_index); | 743 | const sym_index = try func.bin_file.lowerUnnamedConst(.{ .ty = ty, .val = val }, func.decl_index); |
| 747 | break :blk WValue{ .memory = sym_index }; | 744 | break :blk WValue{ .memory = sym_index }; |
| 748 | } else try self.lowerConstant(val, ty); | 745 | } else try func.lowerConstant(val, ty); |
| 749 | | 746 | |
| 750 | gop.value_ptr.* = result; | 747 | gop.value_ptr.* = result; |
| 751 | return result; | 748 | return result; |
| 752 | } | 749 | } |
| 753 | | 750 | |
| 754 | fn finishAir(self: *Self, inst: Air.Inst.Index, result: WValue, operands: []const Air.Inst.Ref) void { | 751 | fn finishAir(func: *CodeGen, inst: Air.Inst.Index, result: WValue, operands: []const Air.Inst.Ref) void { |
| 755 | assert(operands.len <= Liveness.bpi - 1); | 752 | assert(operands.len <= Liveness.bpi - 1); |
| 756 | var tomb_bits = self.liveness.getTombBits(inst); | 753 | var tomb_bits = func.liveness.getTombBits(inst); |
| 757 | for (operands) |operand| { | 754 | for (operands) |operand| { |
| 758 | const dies = @truncate(u1, tomb_bits) != 0; | 755 | const dies = @truncate(u1, tomb_bits) != 0; |
| 759 | tomb_bits >>= 1; | 756 | tomb_bits >>= 1; |
| 760 | if (!dies) continue; | 757 | if (!dies) continue; |
| 761 | processDeath(self, operand); | 758 | processDeath(func, operand); |
| 762 | } | 759 | } |
| 763 | | 760 | |
| 764 | // results of `none` can never be referenced. | 761 | // results of `none` can never be referenced. |
| 765 | if (result != .none) { | 762 | if (result != .none) { |
| 766 | assert(result != .stack); // it's illegal to store a stack value as we cannot track its position | 763 | assert(result != .stack); // it's illegal to store a stack value as we cannot track its position |
| 767 | const branch = self.currentBranch(); | 764 | const branch = func.currentBranch(); |
| 768 | branch.values.putAssumeCapacityNoClobber(Air.indexToRef(inst), result); | 765 | branch.values.putAssumeCapacityNoClobber(Air.indexToRef(inst), result); |
| 769 | } | 766 | } |
| 770 | | 767 | |
| 771 | if (builtin.mode == .Debug) { | 768 | if (builtin.mode == .Debug) { |
| 772 | self.air_bookkeeping += 1; | 769 | func.air_bookkeeping += 1; |
| 773 | } | 770 | } |
| 774 | } | 771 | } |
| 775 | | 772 | |
| ... | @@ -781,12 +778,12 @@ const Branch = struct { | ... | @@ -781,12 +778,12 @@ const Branch = struct { |
| 781 | } | 778 | } |
| 782 | }; | 779 | }; |
| 783 | | 780 | |
| 784 | inline fn currentBranch(self: *Self) *Branch { | 781 | inline fn currentBranch(func: *CodeGen) *Branch { |
| 785 | return &self.branches.items[self.branches.items.len - 1]; | 782 | return &func.branches.items[func.branches.items.len - 1]; |
| 786 | } | 783 | } |
| 787 | | 784 | |
| 788 | const BigTomb = struct { | 785 | const BigTomb = struct { |
| 789 | gen: *Self, | 786 | gen: *CodeGen, |
| 790 | inst: Air.Inst.Index, | 787 | inst: Air.Inst.Index, |
| 791 | lbt: Liveness.BigTomb, | 788 | lbt: Liveness.BigTomb, |
| 792 | | 789 | |
| ... | @@ -809,85 +806,85 @@ const BigTomb = struct { | ... | @@ -809,85 +806,85 @@ const BigTomb = struct { |
| 809 | } | 806 | } |
| 810 | }; | 807 | }; |
| 811 | | 808 | |
| 812 | fn iterateBigTomb(self: *Self, inst: Air.Inst.Index, operand_count: usize) !BigTomb { | 809 | fn iterateBigTomb(func: *CodeGen, inst: Air.Inst.Index, operand_count: usize) !BigTomb { |
| 813 | try self.currentBranch().values.ensureUnusedCapacity(self.gpa, operand_count + 1); | 810 | try func.currentBranch().values.ensureUnusedCapacity(func.gpa, operand_count + 1); |
| 814 | return BigTomb{ | 811 | return BigTomb{ |
| 815 | .gen = self, | 812 | .gen = func, |
| 816 | .inst = inst, | 813 | .inst = inst, |
| 817 | .lbt = self.liveness.iterateBigTomb(inst), | 814 | .lbt = func.liveness.iterateBigTomb(inst), |
| 818 | }; | 815 | }; |
| 819 | } | 816 | } |
| 820 | | 817 | |
| 821 | fn processDeath(self: *Self, ref: Air.Inst.Ref) void { | 818 | fn processDeath(func: *CodeGen, ref: Air.Inst.Ref) void { |
| 822 | const inst = Air.refToIndex(ref) orelse return; | 819 | const inst = Air.refToIndex(ref) orelse return; |
| 823 | if (self.air.instructions.items(.tag)[inst] == .constant) return; | 820 | if (func.air.instructions.items(.tag)[inst] == .constant) return; |
| 824 | // Branches are currently only allowed to free locals allocated | 821 | // Branches are currently only allowed to free locals allocated |
| 825 | // within their own branch. | 822 | // within their own branch. |
| 826 | // TODO: Upon branch consolidation free any locals if needed. | 823 | // TODO: Upon branch consolidation free any locals if needed. |
| 827 | const value = self.currentBranch().values.getPtr(ref) orelse return; | 824 | const value = func.currentBranch().values.getPtr(ref) orelse return; |
| 828 | if (value.* != .local) return; | 825 | if (value.* != .local) return; |
| 829 | log.debug("Decreasing reference for ref: %{?d}\n", .{Air.refToIndex(ref)}); | 826 | log.debug("Decreasing reference for ref: %{?d}\n", .{Air.refToIndex(ref)}); |
| 830 | value.local.references -= 1; // if this panics, a call to `reuseOperand` was forgotten by the developer | 827 | value.local.references -= 1; // if this panics, a call to `reuseOperand` was forgotten by the developer |
| 831 | if (value.local.references == 0) { | 828 | if (value.local.references == 0) { |
| 832 | value.free(self); | 829 | value.free(func); |
| 833 | } | 830 | } |
| 834 | } | 831 | } |
| 835 | | 832 | |
| 836 | /// Appends a MIR instruction and returns its index within the list of instructions | 833 | /// Appends a MIR instruction and returns its index within the list of instructions |
| 837 | fn addInst(self: *Self, inst: Mir.Inst) error{OutOfMemory}!void { | 834 | fn addInst(func: *CodeGen, inst: Mir.Inst) error{OutOfMemory}!void { |
| 838 | try self.mir_instructions.append(self.gpa, inst); | 835 | try func.mir_instructions.append(func.gpa, inst); |
| 839 | } | 836 | } |
| 840 | | 837 | |
| 841 | fn addTag(self: *Self, tag: Mir.Inst.Tag) error{OutOfMemory}!void { | 838 | fn addTag(func: *CodeGen, tag: Mir.Inst.Tag) error{OutOfMemory}!void { |
| 842 | try self.addInst(.{ .tag = tag, .data = .{ .tag = {} } }); | 839 | try func.addInst(.{ .tag = tag, .data = .{ .tag = {} } }); |
| 843 | } | 840 | } |
| 844 | | 841 | |
| 845 | fn addExtended(self: *Self, opcode: wasm.PrefixedOpcode) error{OutOfMemory}!void { | 842 | fn addExtended(func: *CodeGen, opcode: wasm.PrefixedOpcode) error{OutOfMemory}!void { |
| 846 | try self.addInst(.{ .tag = .extended, .secondary = @enumToInt(opcode), .data = .{ .tag = {} } }); | 843 | try func.addInst(.{ .tag = .extended, .secondary = @enumToInt(opcode), .data = .{ .tag = {} } }); |
| 847 | } | 844 | } |
| 848 | | 845 | |
| 849 | fn addLabel(self: *Self, tag: Mir.Inst.Tag, label: u32) error{OutOfMemory}!void { | 846 | fn addLabel(func: *CodeGen, tag: Mir.Inst.Tag, label: u32) error{OutOfMemory}!void { |
| 850 | try self.addInst(.{ .tag = tag, .data = .{ .label = label } }); | 847 | try func.addInst(.{ .tag = tag, .data = .{ .label = label } }); |
| 851 | } | 848 | } |
| 852 | | 849 | |
| 853 | fn addImm32(self: *Self, imm: i32) error{OutOfMemory}!void { | 850 | fn addImm32(func: *CodeGen, imm: i32) error{OutOfMemory}!void { |
| 854 | try self.addInst(.{ .tag = .i32_const, .data = .{ .imm32 = imm } }); | 851 | try func.addInst(.{ .tag = .i32_const, .data = .{ .imm32 = imm } }); |
| 855 | } | 852 | } |
| 856 | | 853 | |
| 857 | /// Accepts an unsigned 64bit integer rather than a signed integer to | 854 | /// Accepts an unsigned 64bit integer rather than a signed integer to |
| 858 | /// prevent us from having to bitcast multiple times as most values | 855 | /// prevent us from having to bitcast multiple times as most values |
| 859 | /// within codegen are represented as unsigned rather than signed. | 856 | /// within codegen are represented as unsigned rather than signed. |
| 860 | fn addImm64(self: *Self, imm: u64) error{OutOfMemory}!void { | 857 | fn addImm64(func: *CodeGen, imm: u64) error{OutOfMemory}!void { |
| 861 | const extra_index = try self.addExtra(Mir.Imm64.fromU64(imm)); | 858 | const extra_index = try func.addExtra(Mir.Imm64.fromU64(imm)); |
| 862 | try self.addInst(.{ .tag = .i64_const, .data = .{ .payload = extra_index } }); | 859 | try func.addInst(.{ .tag = .i64_const, .data = .{ .payload = extra_index } }); |
| 863 | } | 860 | } |
| 864 | | 861 | |
| 865 | fn addFloat64(self: *Self, float: f64) error{OutOfMemory}!void { | 862 | fn addFloat64(func: *CodeGen, float: f64) error{OutOfMemory}!void { |
| 866 | const extra_index = try self.addExtra(Mir.Float64.fromFloat64(float)); | 863 | const extra_index = try func.addExtra(Mir.Float64.fromFloat64(float)); |
| 867 | try self.addInst(.{ .tag = .f64_const, .data = .{ .payload = extra_index } }); | 864 | try func.addInst(.{ .tag = .f64_const, .data = .{ .payload = extra_index } }); |
| 868 | } | 865 | } |
| 869 | | 866 | |
| 870 | /// Inserts an instruction to load/store from/to wasm's linear memory dependent on the given `tag`. | 867 | /// Inserts an instruction to load/store from/to wasm's linear memory dependent on the given `tag`. |
| 871 | fn addMemArg(self: *Self, tag: Mir.Inst.Tag, mem_arg: Mir.MemArg) error{OutOfMemory}!void { | 868 | fn addMemArg(func: *CodeGen, tag: Mir.Inst.Tag, mem_arg: Mir.MemArg) error{OutOfMemory}!void { |
| 872 | const extra_index = try self.addExtra(mem_arg); | 869 | const extra_index = try func.addExtra(mem_arg); |
| 873 | try self.addInst(.{ .tag = tag, .data = .{ .payload = extra_index } }); | 870 | try func.addInst(.{ .tag = tag, .data = .{ .payload = extra_index } }); |
| 874 | } | 871 | } |
| 875 | | 872 | |
| 876 | /// Appends entries to `mir_extra` based on the type of `extra`. | 873 | /// Appends entries to `mir_extra` based on the type of `extra`. |
| 877 | /// Returns the index into `mir_extra` | 874 | /// Returns the index into `mir_extra` |
| 878 | fn addExtra(self: *Self, extra: anytype) error{OutOfMemory}!u32 { | 875 | fn addExtra(func: *CodeGen, extra: anytype) error{OutOfMemory}!u32 { |
| 879 | const fields = std.meta.fields(@TypeOf(extra)); | 876 | const fields = std.meta.fields(@TypeOf(extra)); |
| 880 | try self.mir_extra.ensureUnusedCapacity(self.gpa, fields.len); | 877 | try func.mir_extra.ensureUnusedCapacity(func.gpa, fields.len); |
| 881 | return self.addExtraAssumeCapacity(extra); | 878 | return func.addExtraAssumeCapacity(extra); |
| 882 | } | 879 | } |
| 883 | | 880 | |
| 884 | /// Appends entries to `mir_extra` based on the type of `extra`. | 881 | /// Appends entries to `mir_extra` based on the type of `extra`. |
| 885 | /// Returns the index into `mir_extra` | 882 | /// Returns the index into `mir_extra` |
| 886 | fn addExtraAssumeCapacity(self: *Self, extra: anytype) error{OutOfMemory}!u32 { | 883 | fn addExtraAssumeCapacity(func: *CodeGen, extra: anytype) error{OutOfMemory}!u32 { |
| 887 | const fields = std.meta.fields(@TypeOf(extra)); | 884 | const fields = std.meta.fields(@TypeOf(extra)); |
| 888 | const result = @intCast(u32, self.mir_extra.items.len); | 885 | const result = @intCast(u32, func.mir_extra.items.len); |
| 889 | inline for (fields) |field| { | 886 | inline for (fields) |field| { |
| 890 | self.mir_extra.appendAssumeCapacity(switch (field.field_type) { | 887 | func.mir_extra.appendAssumeCapacity(switch (field.field_type) { |
| 891 | u32 => @field(extra, field.name), | 888 | u32 => @field(extra, field.name), |
| 892 | else => |field_type| @compileError("Unsupported field type " ++ @typeName(field_type)), | 889 | else => |field_type| @compileError("Unsupported field type " ++ @typeName(field_type)), |
| 893 | }); | 890 | }); |
| ... | @@ -932,32 +929,32 @@ fn genBlockType(ty: Type, target: std.Target) u8 { | ... | @@ -932,32 +929,32 @@ fn genBlockType(ty: Type, target: std.Target) u8 { |
| 932 | } | 929 | } |
| 933 | | 930 | |
| 934 | /// Writes the bytecode depending on the given `WValue` in `val` | 931 | /// Writes the bytecode depending on the given `WValue` in `val` |
| 935 | fn emitWValue(self: *Self, value: WValue) InnerError!void { | 932 | fn emitWValue(func: *CodeGen, value: WValue) InnerError!void { |
| 936 | switch (value) { | 933 | switch (value) { |
| 937 | .none, .stack => {}, // no-op | 934 | .none, .stack => {}, // no-op |
| 938 | .local => |idx| try self.addLabel(.local_get, idx.value), | 935 | .local => |idx| try func.addLabel(.local_get, idx.value), |
| 939 | .imm32 => |val| try self.addImm32(@bitCast(i32, val)), | 936 | .imm32 => |val| try func.addImm32(@bitCast(i32, val)), |
| 940 | .imm64 => |val| try self.addImm64(val), | 937 | .imm64 => |val| try func.addImm64(val), |
| 941 | .float32 => |val| try self.addInst(.{ .tag = .f32_const, .data = .{ .float32 = val } }), | 938 | .float32 => |val| try func.addInst(.{ .tag = .f32_const, .data = .{ .float32 = val } }), |
| 942 | .float64 => |val| try self.addFloat64(val), | 939 | .float64 => |val| try func.addFloat64(val), |
| 943 | .memory => |ptr| { | 940 | .memory => |ptr| { |
| 944 | const extra_index = try self.addExtra(Mir.Memory{ .pointer = ptr, .offset = 0 }); | 941 | const extra_index = try func.addExtra(Mir.Memory{ .pointer = ptr, .offset = 0 }); |
| 945 | try self.addInst(.{ .tag = .memory_address, .data = .{ .payload = extra_index } }); | 942 | try func.addInst(.{ .tag = .memory_address, .data = .{ .payload = extra_index } }); |
| 946 | }, | 943 | }, |
| 947 | .memory_offset => |mem_off| { | 944 | .memory_offset => |mem_off| { |
| 948 | const extra_index = try self.addExtra(Mir.Memory{ .pointer = mem_off.pointer, .offset = mem_off.offset }); | 945 | const extra_index = try func.addExtra(Mir.Memory{ .pointer = mem_off.pointer, .offset = mem_off.offset }); |
| 949 | try self.addInst(.{ .tag = .memory_address, .data = .{ .payload = extra_index } }); | 946 | try func.addInst(.{ .tag = .memory_address, .data = .{ .payload = extra_index } }); |
| 950 | }, | 947 | }, |
| 951 | .function_index => |index| try self.addLabel(.function_index, index), // write function index and generate relocation | 948 | .function_index => |index| try func.addLabel(.function_index, index), // write function index and generate relocation |
| 952 | .stack_offset => try self.addLabel(.local_get, self.bottom_stack_value.local.value), // caller must ensure to address the offset | 949 | .stack_offset => try func.addLabel(.local_get, func.bottom_stack_value.local.value), // caller must ensure to address the offset |
| 953 | } | 950 | } |
| 954 | } | 951 | } |
| 955 | | 952 | |
| 956 | /// If given a local or stack-offset, increases the reference count by 1. | 953 | /// If given a local or stack-offset, increases the reference count by 1. |
| 957 | /// The old `WValue` found at instruction `ref` is then replaced by the | 954 | /// The old `WValue` found at instruction `ref` is then replaced by the |
| 958 | /// modified `WValue` and returned. When given a non-local or non-stack-offset, | 955 | /// modified `WValue` and returned. When given a non-local or non-stack-offset, |
| 959 | /// returns the given `operand` itself instead. | 956 | /// returns the given `operand` itfunc instead. |
| 960 | fn reuseOperand(self: *Self, ref: Air.Inst.Ref, operand: WValue) WValue { | 957 | fn reuseOperand(func: *CodeGen, ref: Air.Inst.Ref, operand: WValue) WValue { |
| 961 | if (operand != .local and operand != .stack_offset) return operand; | 958 | if (operand != .local and operand != .stack_offset) return operand; |
| 962 | var new_value = operand; | 959 | var new_value = operand; |
| 963 | switch (new_value) { | 960 | switch (new_value) { |
| ... | @@ -965,17 +962,17 @@ fn reuseOperand(self: *Self, ref: Air.Inst.Ref, operand: WValue) WValue { | ... | @@ -965,17 +962,17 @@ fn reuseOperand(self: *Self, ref: Air.Inst.Ref, operand: WValue) WValue { |
| 965 | .stack_offset => |*stack_offset| stack_offset.references += 1, | 962 | .stack_offset => |*stack_offset| stack_offset.references += 1, |
| 966 | else => unreachable, | 963 | else => unreachable, |
| 967 | } | 964 | } |
| 968 | const old_value = self.getResolvedInst(ref); | 965 | const old_value = func.getResolvedInst(ref); |
| 969 | old_value.* = new_value; | 966 | old_value.* = new_value; |
| 970 | return new_value; | 967 | return new_value; |
| 971 | } | 968 | } |
| 972 | | 969 | |
| 973 | /// From a reference, returns its resolved `WValue`. | 970 | /// From a reference, returns its resolved `WValue`. |
| 974 | /// It's illegal to provide a `Air.Inst.Ref` that hasn't been resolved yet. | 971 | /// It's illegal to provide a `Air.Inst.Ref` that hasn't been resolved yet. |
| 975 | fn getResolvedInst(self: *Self, ref: Air.Inst.Ref) *WValue { | 972 | fn getResolvedInst(func: *CodeGen, ref: Air.Inst.Ref) *WValue { |
| 976 | var index = self.branches.items.len; | 973 | var index = func.branches.items.len; |
| 977 | while (index > 0) : (index -= 1) { | 974 | while (index > 0) : (index -= 1) { |
| 978 | const branch = self.branches.items[index - 1]; | 975 | const branch = func.branches.items[index - 1]; |
| 979 | if (branch.values.getPtr(ref)) |value| { | 976 | if (branch.values.getPtr(ref)) |value| { |
| 980 | return value; | 977 | return value; |
| 981 | } | 978 | } |
| ... | @@ -985,37 +982,37 @@ fn getResolvedInst(self: *Self, ref: Air.Inst.Ref) *WValue { | ... | @@ -985,37 +982,37 @@ fn getResolvedInst(self: *Self, ref: Air.Inst.Ref) *WValue { |
| 985 | | 982 | |
| 986 | /// Creates one locals for a given `Type`. | 983 | /// Creates one locals for a given `Type`. |
| 987 | /// Returns a corresponding `Wvalue` with `local` as active tag | 984 | /// Returns a corresponding `Wvalue` with `local` as active tag |
| 988 | fn allocLocal(self: *Self, ty: Type) InnerError!WValue { | 985 | fn allocLocal(func: *CodeGen, ty: Type) InnerError!WValue { |
| 989 | const valtype = typeToValtype(ty, self.target); | 986 | const valtype = typeToValtype(ty, func.target); |
| 990 | switch (valtype) { | 987 | switch (valtype) { |
| 991 | .i32 => if (self.free_locals_i32.popOrNull()) |index| { | 988 | .i32 => if (func.free_locals_i32.popOrNull()) |index| { |
| 992 | log.debug("reusing local ({d}) of type {}\n", .{ index, valtype }); | 989 | log.debug("reusing local ({d}) of type {}\n", .{ index, valtype }); |
| 993 | return WValue{ .local = .{ .value = index, .references = 1 } }; | 990 | return WValue{ .local = .{ .value = index, .references = 1 } }; |
| 994 | }, | 991 | }, |
| 995 | .i64 => if (self.free_locals_i64.popOrNull()) |index| { | 992 | .i64 => if (func.free_locals_i64.popOrNull()) |index| { |
| 996 | log.debug("reusing local ({d}) of type {}\n", .{ index, valtype }); | 993 | log.debug("reusing local ({d}) of type {}\n", .{ index, valtype }); |
| 997 | return WValue{ .local = .{ .value = index, .references = 1 } }; | 994 | return WValue{ .local = .{ .value = index, .references = 1 } }; |
| 998 | }, | 995 | }, |
| 999 | .f32 => if (self.free_locals_f32.popOrNull()) |index| { | 996 | .f32 => if (func.free_locals_f32.popOrNull()) |index| { |
| 1000 | log.debug("reusing local ({d}) of type {}\n", .{ index, valtype }); | 997 | log.debug("reusing local ({d}) of type {}\n", .{ index, valtype }); |
| 1001 | return WValue{ .local = .{ .value = index, .references = 1 } }; | 998 | return WValue{ .local = .{ .value = index, .references = 1 } }; |
| 1002 | }, | 999 | }, |
| 1003 | .f64 => if (self.free_locals_f64.popOrNull()) |index| { | 1000 | .f64 => if (func.free_locals_f64.popOrNull()) |index| { |
| 1004 | log.debug("reusing local ({d}) of type {}\n", .{ index, valtype }); | 1001 | log.debug("reusing local ({d}) of type {}\n", .{ index, valtype }); |
| 1005 | return WValue{ .local = .{ .value = index, .references = 1 } }; | 1002 | return WValue{ .local = .{ .value = index, .references = 1 } }; |
| 1006 | }, | 1003 | }, |
| 1007 | } | 1004 | } |
| 1008 | log.debug("new local of type {}\n", .{valtype}); | 1005 | log.debug("new local of type {}\n", .{valtype}); |
| 1009 | // no local was free to be re-used, so allocate a new local instead | 1006 | // no local was free to be re-used, so allocate a new local instead |
| 1010 | return self.ensureAllocLocal(ty); | 1007 | return func.ensureAllocLocal(ty); |
| 1011 | } | 1008 | } |
| 1012 | | 1009 | |
| 1013 | /// Ensures a new local will be created. This is useful when it's useful | 1010 | /// Ensures a new local will be created. This is useful when it's useful |
| 1014 | /// to use a zero-initialized local. | 1011 | /// to use a zero-initialized local. |
| 1015 | fn ensureAllocLocal(self: *Self, ty: Type) InnerError!WValue { | 1012 | fn ensureAllocLocal(func: *CodeGen, ty: Type) InnerError!WValue { |
| 1016 | try self.locals.append(self.gpa, genValtype(ty, self.target)); | 1013 | try func.locals.append(func.gpa, genValtype(ty, func.target)); |
| 1017 | const initial_index = self.local_index; | 1014 | const initial_index = func.local_index; |
| 1018 | self.local_index += 1; | 1015 | func.local_index += 1; |
| 1019 | return WValue{ .local = .{ .value = initial_index, .references = 1 } }; | 1016 | return WValue{ .local = .{ .value = initial_index, .references = 1 } }; |
| 1020 | } | 1017 | } |
| 1021 | | 1018 | |
| ... | @@ -1082,7 +1079,7 @@ pub fn generate( | ... | @@ -1082,7 +1079,7 @@ pub fn generate( |
| 1082 | debug_output: codegen.DebugInfoOutput, | 1079 | debug_output: codegen.DebugInfoOutput, |
| 1083 | ) codegen.GenerateSymbolError!codegen.FnResult { | 1080 | ) codegen.GenerateSymbolError!codegen.FnResult { |
| 1084 | _ = src_loc; | 1081 | _ = src_loc; |
| 1085 | var code_gen: Self = .{ | 1082 | var code_gen: CodeGen = .{ |
| 1086 | .gpa = bin_file.allocator, | 1083 | .gpa = bin_file.allocator, |
| 1087 | .air = air, | 1084 | .air = air, |
| 1088 | .liveness = liveness, | 1085 | .liveness = liveness, |
| ... | @@ -1107,88 +1104,89 @@ pub fn generate( | ... | @@ -1107,88 +1104,89 @@ pub fn generate( |
| 1107 | return codegen.FnResult{ .appended = {} }; | 1104 | return codegen.FnResult{ .appended = {} }; |
| 1108 | } | 1105 | } |
| 1109 | | 1106 | |
| 1110 | fn genFunc(self: *Self) InnerError!void { | 1107 | fn genFunc(func: *CodeGen) InnerError!void { |
| 1111 | const fn_info = self.decl.ty.fnInfo(); | 1108 | const fn_info = func.decl.ty.fnInfo(); |
| 1112 | var func_type = try genFunctype(self.gpa, fn_info.cc, fn_info.param_types, fn_info.return_type, self.target); | 1109 | var func_type = try genFunctype(func.gpa, fn_info.cc, fn_info.param_types, fn_info.return_type, func.target); |
| 1113 | defer func_type.deinit(self.gpa); | 1110 | defer func_type.deinit(func.gpa); |
| 1114 | self.decl.fn_link.wasm.type_index = try self.bin_file.putOrGetFuncType(func_type); | 1111 | func.decl.fn_link.wasm.type_index = try func.bin_file.putOrGetFuncType(func_type); |
| 1115 | | 1112 | |
| 1116 | var cc_result = try self.resolveCallingConventionValues(self.decl.ty); | 1113 | var cc_result = try func.resolveCallingConventionValues(func.decl.ty); |
| 1117 | defer cc_result.deinit(self.gpa); | 1114 | defer cc_result.deinit(func.gpa); |
| 1118 | | 1115 | |
| 1119 | self.args = cc_result.args; | 1116 | func.args = cc_result.args; |
| 1120 | self.return_value = cc_result.return_value; | 1117 | func.return_value = cc_result.return_value; |
| 1121 | | 1118 | |
| 1122 | try self.addTag(.dbg_prologue_end); | 1119 | try func.addTag(.dbg_prologue_end); |
| 1123 | | 1120 | |
| 1124 | try self.branches.append(self.gpa, .{}); | 1121 | try func.branches.append(func.gpa, .{}); |
| 1125 | // Generate MIR for function body | 1122 | // Generate MIR for function body |
| 1126 | try self.genBody(self.air.getMainBody()); | 1123 | try func.genBody(func.air.getMainBody()); |
| 1127 | | 1124 | |
| 1128 | // clean up outer branch | 1125 | // clean up outer branch |
| 1129 | _ = self.branches.pop(); | 1126 | var outer_branch = func.branches.pop(); |
| | 1127 | outer_branch.deinit(func.gpa); |
| 1130 | | 1128 | |
| 1131 | // In case we have a return value, but the last instruction is a noreturn (such as a while loop) | 1129 | // In case we have a return value, but the last instruction is a noreturn (such as a while loop) |
| 1132 | // we emit an unreachable instruction to tell the stack validator that part will never be reached. | 1130 | // we emit an unreachable instruction to tell the stack validator that part will never be reached. |
| 1133 | if (func_type.returns.len != 0 and self.air.instructions.len > 0) { | 1131 | if (func_type.returns.len != 0 and func.air.instructions.len > 0) { |
| 1134 | const inst = @intCast(u32, self.air.instructions.len - 1); | 1132 | const inst = @intCast(u32, func.air.instructions.len - 1); |
| 1135 | const last_inst_ty = self.air.typeOfIndex(inst); | 1133 | const last_inst_ty = func.air.typeOfIndex(inst); |
| 1136 | if (!last_inst_ty.hasRuntimeBitsIgnoreComptime() or last_inst_ty.isNoReturn()) { | 1134 | if (!last_inst_ty.hasRuntimeBitsIgnoreComptime() or last_inst_ty.isNoReturn()) { |
| 1137 | try self.addTag(.@"unreachable"); | 1135 | try func.addTag(.@"unreachable"); |
| 1138 | } | 1136 | } |
| 1139 | } | 1137 | } |
| 1140 | // End of function body | 1138 | // End of function body |
| 1141 | try self.addTag(.end); | 1139 | try func.addTag(.end); |
| 1142 | | 1140 | |
| 1143 | try self.addTag(.dbg_epilogue_begin); | 1141 | try func.addTag(.dbg_epilogue_begin); |
| 1144 | | 1142 | |
| 1145 | // check if we have to initialize and allocate anything into the stack frame. | 1143 | // check if we have to initialize and allocate anything into the stack frame. |
| 1146 | // If so, create enough stack space and insert the instructions at the front of the list. | 1144 | // If so, create enough stack space and insert the instructions at the front of the list. |
| 1147 | if (self.stack_size > 0) { | 1145 | if (func.stack_size > 0) { |
| 1148 | var prologue = std.ArrayList(Mir.Inst).init(self.gpa); | 1146 | var prologue = std.ArrayList(Mir.Inst).init(func.gpa); |
| 1149 | defer prologue.deinit(); | 1147 | defer prologue.deinit(); |
| 1150 | | 1148 | |
| 1151 | // load stack pointer | 1149 | // load stack pointer |
| 1152 | try prologue.append(.{ .tag = .global_get, .data = .{ .label = 0 } }); | 1150 | try prologue.append(.{ .tag = .global_get, .data = .{ .label = 0 } }); |
| 1153 | // store stack pointer so we can restore it when we return from the function | 1151 | // store stack pointer so we can restore it when we return from the function |
| 1154 | try prologue.append(.{ .tag = .local_tee, .data = .{ .label = self.initial_stack_value.local.value } }); | 1152 | try prologue.append(.{ .tag = .local_tee, .data = .{ .label = func.initial_stack_value.local.value } }); |
| 1155 | // get the total stack size | 1153 | // get the total stack size |
| 1156 | const aligned_stack = std.mem.alignForwardGeneric(u32, self.stack_size, self.stack_alignment); | 1154 | const aligned_stack = std.mem.alignForwardGeneric(u32, func.stack_size, func.stack_alignment); |
| 1157 | try prologue.append(.{ .tag = .i32_const, .data = .{ .imm32 = @intCast(i32, aligned_stack) } }); | 1155 | try prologue.append(.{ .tag = .i32_const, .data = .{ .imm32 = @intCast(i32, aligned_stack) } }); |
| 1158 | // substract it from the current stack pointer | 1156 | // substract it from the current stack pointer |
| 1159 | try prologue.append(.{ .tag = .i32_sub, .data = .{ .tag = {} } }); | 1157 | try prologue.append(.{ .tag = .i32_sub, .data = .{ .tag = {} } }); |
| 1160 | // Get negative stack aligment | 1158 | // Get negative stack aligment |
| 1161 | try prologue.append(.{ .tag = .i32_const, .data = .{ .imm32 = @intCast(i32, self.stack_alignment) * -1 } }); | 1159 | try prologue.append(.{ .tag = .i32_const, .data = .{ .imm32 = @intCast(i32, func.stack_alignment) * -1 } }); |
| 1162 | // Bitwise-and the value to get the new stack pointer to ensure the pointers are aligned with the abi alignment | 1160 | // Bitwise-and the value to get the new stack pointer to ensure the pointers are aligned with the abi alignment |
| 1163 | try prologue.append(.{ .tag = .i32_and, .data = .{ .tag = {} } }); | 1161 | try prologue.append(.{ .tag = .i32_and, .data = .{ .tag = {} } }); |
| 1164 | // store the current stack pointer as the bottom, which will be used to calculate all stack pointer offsets | 1162 | // store the current stack pointer as the bottom, which will be used to calculate all stack pointer offsets |
| 1165 | try prologue.append(.{ .tag = .local_tee, .data = .{ .label = self.bottom_stack_value.local.value } }); | 1163 | try prologue.append(.{ .tag = .local_tee, .data = .{ .label = func.bottom_stack_value.local.value } }); |
| 1166 | // Store the current stack pointer value into the global stack pointer so other function calls will | 1164 | // Store the current stack pointer value into the global stack pointer so other function calls will |
| 1167 | // start from this value instead and not overwrite the current stack. | 1165 | // start from this value instead and not overwrite the current stack. |
| 1168 | try prologue.append(.{ .tag = .global_set, .data = .{ .label = 0 } }); | 1166 | try prologue.append(.{ .tag = .global_set, .data = .{ .label = 0 } }); |
| 1169 | | 1167 | |
| 1170 | // reserve space and insert all prologue instructions at the front of the instruction list | 1168 | // reserve space and insert all prologue instructions at the front of the instruction list |
| 1171 | // We insert them in reserve order as there is no insertSlice in multiArrayList. | 1169 | // We insert them in reserve order as there is no insertSlice in multiArrayList. |
| 1172 | try self.mir_instructions.ensureUnusedCapacity(self.gpa, prologue.items.len); | 1170 | try func.mir_instructions.ensureUnusedCapacity(func.gpa, prologue.items.len); |
| 1173 | for (prologue.items) |_, index| { | 1171 | for (prologue.items) |_, index| { |
| 1174 | const inst = prologue.items[prologue.items.len - 1 - index]; | 1172 | const inst = prologue.items[prologue.items.len - 1 - index]; |
| 1175 | self.mir_instructions.insertAssumeCapacity(0, inst); | 1173 | func.mir_instructions.insertAssumeCapacity(0, inst); |
| 1176 | } | 1174 | } |
| 1177 | } | 1175 | } |
| 1178 | | 1176 | |
| 1179 | var mir: Mir = .{ | 1177 | var mir: Mir = .{ |
| 1180 | .instructions = self.mir_instructions.toOwnedSlice(), | 1178 | .instructions = func.mir_instructions.toOwnedSlice(), |
| 1181 | .extra = self.mir_extra.toOwnedSlice(self.gpa), | 1179 | .extra = func.mir_extra.toOwnedSlice(func.gpa), |
| 1182 | }; | 1180 | }; |
| 1183 | defer mir.deinit(self.gpa); | 1181 | defer mir.deinit(func.gpa); |
| 1184 | | 1182 | |
| 1185 | var emit: Emit = .{ | 1183 | var emit: Emit = .{ |
| 1186 | .mir = mir, | 1184 | .mir = mir, |
| 1187 | .bin_file = &self.bin_file.base, | 1185 | .bin_file = &func.bin_file.base, |
| 1188 | .code = self.code, | 1186 | .code = func.code, |
| 1189 | .locals = self.locals.items, | 1187 | .locals = func.locals.items, |
| 1190 | .decl = self.decl, | 1188 | .decl = func.decl, |
| 1191 | .dbg_output = self.debug_output, | 1189 | .dbg_output = func.debug_output, |
| 1192 | .prev_di_line = 0, | 1190 | .prev_di_line = 0, |
| 1193 | .prev_di_column = 0, | 1191 | .prev_di_column = 0, |
| 1194 | .prev_di_offset = 0, | 1192 | .prev_di_offset = 0, |
| ... | @@ -1196,7 +1194,7 @@ fn genFunc(self: *Self) InnerError!void { | ... | @@ -1196,7 +1194,7 @@ fn genFunc(self: *Self) InnerError!void { |
| 1196 | | 1194 | |
| 1197 | emit.emitMir() catch |err| switch (err) { | 1195 | emit.emitMir() catch |err| switch (err) { |
| 1198 | error.EmitFail => { | 1196 | error.EmitFail => { |
| 1199 | self.err_msg = emit.error_msg.?; | 1197 | func.err_msg = emit.error_msg.?; |
| 1200 | return error.CodegenFail; | 1198 | return error.CodegenFail; |
| 1201 | }, | 1199 | }, |
| 1202 | else => |e| return e, | 1200 | else => |e| return e, |
| ... | @@ -1207,16 +1205,16 @@ const CallWValues = struct { | ... | @@ -1207,16 +1205,16 @@ const CallWValues = struct { |
| 1207 | args: []WValue, | 1205 | args: []WValue, |
| 1208 | return_value: WValue, | 1206 | return_value: WValue, |
| 1209 | | 1207 | |
| 1210 | fn deinit(self: *CallWValues, gpa: Allocator) void { | 1208 | fn deinit(values: *CallWValues, gpa: Allocator) void { |
| 1211 | gpa.free(self.args); | 1209 | gpa.free(values.args); |
| 1212 | self.* = undefined; | 1210 | values.* = undefined; |
| 1213 | } | 1211 | } |
| 1214 | }; | 1212 | }; |
| 1215 | | 1213 | |
| 1216 | fn resolveCallingConventionValues(self: *Self, fn_ty: Type) InnerError!CallWValues { | 1214 | fn resolveCallingConventionValues(func: *CodeGen, fn_ty: Type) InnerError!CallWValues { |
| 1217 | const cc = fn_ty.fnCallingConvention(); | 1215 | const cc = fn_ty.fnCallingConvention(); |
| 1218 | const param_types = try self.gpa.alloc(Type, fn_ty.fnParamLen()); | 1216 | const param_types = try func.gpa.alloc(Type, fn_ty.fnParamLen()); |
| 1219 | defer self.gpa.free(param_types); | 1217 | defer func.gpa.free(param_types); |
| 1220 | fn_ty.fnParamTypes(param_types); | 1218 | fn_ty.fnParamTypes(param_types); |
| 1221 | var result: CallWValues = .{ | 1219 | var result: CallWValues = .{ |
| 1222 | .args = &.{}, | 1220 | .args = &.{}, |
| ... | @@ -1224,17 +1222,17 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) InnerError!CallWValu | ... | @@ -1224,17 +1222,17 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) InnerError!CallWValu |
| 1224 | }; | 1222 | }; |
| 1225 | if (cc == .Naked) return result; | 1223 | if (cc == .Naked) return result; |
| 1226 | | 1224 | |
| 1227 | var args = std.ArrayList(WValue).init(self.gpa); | 1225 | var args = std.ArrayList(WValue).init(func.gpa); |
| 1228 | defer args.deinit(); | 1226 | defer args.deinit(); |
| 1229 | | 1227 | |
| 1230 | // Check if we store the result as a pointer to the stack rather than | 1228 | // Check if we store the result as a pointer to the stack rather than |
| 1231 | // by value | 1229 | // by value |
| 1232 | const fn_info = fn_ty.fnInfo(); | 1230 | const fn_info = fn_ty.fnInfo(); |
| 1233 | if (firstParamSRet(fn_info.cc, fn_info.return_type, self.target)) { | 1231 | if (firstParamSRet(fn_info.cc, fn_info.return_type, func.target)) { |
| 1234 | // the sret arg will be passed as first argument, therefore we | 1232 | // the sret arg will be passed as first argument, therefore we |
| 1235 | // set the `return_value` before allocating locals for regular args. | 1233 | // set the `return_value` before allocating locals for regular args. |
| 1236 | result.return_value = .{ .local = .{ .value = self.local_index, .references = 1 } }; | 1234 | result.return_value = .{ .local = .{ .value = func.local_index, .references = 1 } }; |
| 1237 | self.local_index += 1; | 1235 | func.local_index += 1; |
| 1238 | } | 1236 | } |
| 1239 | | 1237 | |
| 1240 | switch (cc) { | 1238 | switch (cc) { |
| ... | @@ -1244,21 +1242,21 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) InnerError!CallWValu | ... | @@ -1244,21 +1242,21 @@ fn resolveCallingConventionValues(self: *Self, fn_ty: Type) InnerError!CallWValu |
| 1244 | continue; | 1242 | continue; |
| 1245 | } | 1243 | } |
| 1246 | | 1244 | |
| 1247 | try args.append(.{ .local = .{ .value = self.local_index, .references = 1 } }); | 1245 | try args.append(.{ .local = .{ .value = func.local_index, .references = 1 } }); |
| 1248 | self.local_index += 1; | 1246 | func.local_index += 1; |
| 1249 | } | 1247 | } |
| 1250 | }, | 1248 | }, |
| 1251 | .C => { | 1249 | .C => { |
| 1252 | for (param_types) |ty| { | 1250 | for (param_types) |ty| { |
| 1253 | const ty_classes = abi.classifyType(ty, self.target); | 1251 | const ty_classes = abi.classifyType(ty, func.target); |
| 1254 | for (ty_classes) |class| { | 1252 | for (ty_classes) |class| { |
| 1255 | if (class == .none) continue; | 1253 | if (class == .none) continue; |
| 1256 | try args.append(.{ .local = .{ .value = self.local_index, .references = 1 } }); | 1254 | try args.append(.{ .local = .{ .value = func.local_index, .references = 1 } }); |
| 1257 | self.local_index += 1; | 1255 | func.local_index += 1; |
| 1258 | } | 1256 | } |
| 1259 | } | 1257 | } |
| 1260 | }, | 1258 | }, |
| 1261 | else => return self.fail("calling convention '{s}' not supported for Wasm", .{@tagName(cc)}), | 1259 | else => return func.fail("calling convention '{s}' not supported for Wasm", .{@tagName(cc)}), |
| 1262 | } | 1260 | } |
| 1263 | result.args = args.toOwnedSlice(); | 1261 | result.args = args.toOwnedSlice(); |
| 1264 | return result; | 1262 | return result; |
| ... | @@ -1279,14 +1277,14 @@ fn firstParamSRet(cc: std.builtin.CallingConvention, return_type: Type, target: | ... | @@ -1279,14 +1277,14 @@ fn firstParamSRet(cc: std.builtin.CallingConvention, return_type: Type, target: |
| 1279 | | 1277 | |
| 1280 | /// For a given `Type`, add debug information to .debug_info at the current position. | 1278 | /// For a given `Type`, add debug information to .debug_info at the current position. |
| 1281 | /// The actual bytes will be written to the position after relocation. | 1279 | /// The actual bytes will be written to the position after relocation. |
| 1282 | fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void { | 1280 | fn addDbgInfoTypeReloc(func: *CodeGen, ty: Type) !void { |
| 1283 | switch (self.debug_output) { | 1281 | switch (func.debug_output) { |
| 1284 | .dwarf => |dwarf| { | 1282 | .dwarf => |dwarf| { |
| 1285 | assert(ty.hasRuntimeBitsIgnoreComptime()); | 1283 | assert(ty.hasRuntimeBitsIgnoreComptime()); |
| 1286 | const dbg_info = &dwarf.dbg_info; | 1284 | const dbg_info = &dwarf.dbg_info; |
| 1287 | const index = dbg_info.items.len; | 1285 | const index = dbg_info.items.len; |
| 1288 | try dbg_info.resize(index + 4); | 1286 | try dbg_info.resize(index + 4); |
| 1289 | const atom = &self.decl.link.wasm.dbg_info_atom; | 1287 | const atom = &func.decl.link.wasm.dbg_info_atom; |
| 1290 | try dwarf.addTypeRelocGlobal(atom, ty, @intCast(u32, index)); | 1288 | try dwarf.addTypeRelocGlobal(atom, ty, @intCast(u32, index)); |
| 1291 | }, | 1289 | }, |
| 1292 | .plan9 => unreachable, | 1290 | .plan9 => unreachable, |
| ... | @@ -1296,96 +1294,96 @@ fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void { | ... | @@ -1296,96 +1294,96 @@ fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void { |
| 1296 | | 1294 | |
| 1297 | /// Lowers a Zig type and its value based on a given calling convention to ensure | 1295 | /// Lowers a Zig type and its value based on a given calling convention to ensure |
| 1298 | /// it matches the ABI. | 1296 | /// it matches the ABI. |
| 1299 | fn lowerArg(self: *Self, cc: std.builtin.CallingConvention, ty: Type, value: WValue) !void { | 1297 | fn lowerArg(func: *CodeGen, cc: std.builtin.CallingConvention, ty: Type, value: WValue) !void { |
| 1300 | if (cc != .C) { | 1298 | if (cc != .C) { |
| 1301 | return self.lowerToStack(value); | 1299 | return func.lowerToStack(value); |
| 1302 | } | 1300 | } |
| 1303 | | 1301 | |
| 1304 | const ty_classes = abi.classifyType(ty, self.target); | 1302 | const ty_classes = abi.classifyType(ty, func.target); |
| 1305 | assert(ty_classes[0] != .none); | 1303 | assert(ty_classes[0] != .none); |
| 1306 | switch (ty.zigTypeTag()) { | 1304 | switch (ty.zigTypeTag()) { |
| 1307 | .Struct, .Union => { | 1305 | .Struct, .Union => { |
| 1308 | if (ty_classes[0] == .indirect) { | 1306 | if (ty_classes[0] == .indirect) { |
| 1309 | return self.lowerToStack(value); | 1307 | return func.lowerToStack(value); |
| 1310 | } | 1308 | } |
| 1311 | assert(ty_classes[0] == .direct); | 1309 | assert(ty_classes[0] == .direct); |
| 1312 | const scalar_type = abi.scalarType(ty, self.target); | 1310 | const scalar_type = abi.scalarType(ty, func.target); |
| 1313 | const abi_size = scalar_type.abiSize(self.target); | 1311 | const abi_size = scalar_type.abiSize(func.target); |
| 1314 | const opcode = buildOpcode(.{ | 1312 | const opcode = buildOpcode(.{ |
| 1315 | .op = .load, | 1313 | .op = .load, |
| 1316 | .width = @intCast(u8, abi_size), | 1314 | .width = @intCast(u8, abi_size), |
| 1317 | .signedness = if (scalar_type.isSignedInt()) .signed else .unsigned, | 1315 | .signedness = if (scalar_type.isSignedInt()) .signed else .unsigned, |
| 1318 | .valtype1 = typeToValtype(scalar_type, self.target), | 1316 | .valtype1 = typeToValtype(scalar_type, func.target), |
| 1319 | }); | 1317 | }); |
| 1320 | try self.emitWValue(value); | 1318 | try func.emitWValue(value); |
| 1321 | try self.addMemArg(Mir.Inst.Tag.fromOpcode(opcode), .{ | 1319 | try func.addMemArg(Mir.Inst.Tag.fromOpcode(opcode), .{ |
| 1322 | .offset = value.offset(), | 1320 | .offset = value.offset(), |
| 1323 | .alignment = scalar_type.abiAlignment(self.target), | 1321 | .alignment = scalar_type.abiAlignment(func.target), |
| 1324 | }); | 1322 | }); |
| 1325 | }, | 1323 | }, |
| 1326 | .Int, .Float => { | 1324 | .Int, .Float => { |
| 1327 | if (ty_classes[1] == .none) { | 1325 | if (ty_classes[1] == .none) { |
| 1328 | return self.lowerToStack(value); | 1326 | return func.lowerToStack(value); |
| 1329 | } | 1327 | } |
| 1330 | assert(ty_classes[0] == .direct and ty_classes[1] == .direct); | 1328 | assert(ty_classes[0] == .direct and ty_classes[1] == .direct); |
| 1331 | assert(ty.abiSize(self.target) == 16); | 1329 | assert(ty.abiSize(func.target) == 16); |
| 1332 | // in this case we have an integer or float that must be lowered as 2 i64's. | 1330 | // in this case we have an integer or float that must be lowered as 2 i64's. |
| 1333 | try self.emitWValue(value); | 1331 | try func.emitWValue(value); |
| 1334 | try self.addMemArg(.i64_load, .{ .offset = value.offset(), .alignment = 8 }); | 1332 | try func.addMemArg(.i64_load, .{ .offset = value.offset(), .alignment = 8 }); |
| 1335 | try self.emitWValue(value); | 1333 | try func.emitWValue(value); |
| 1336 | try self.addMemArg(.i64_load, .{ .offset = value.offset() + 8, .alignment = 8 }); | 1334 | try func.addMemArg(.i64_load, .{ .offset = value.offset() + 8, .alignment = 8 }); |
| 1337 | }, | 1335 | }, |
| 1338 | else => return self.lowerToStack(value), | 1336 | else => return func.lowerToStack(value), |
| 1339 | } | 1337 | } |
| 1340 | } | 1338 | } |
| 1341 | | 1339 | |
| 1342 | /// Lowers a `WValue` to the stack. This means when the `value` results in | 1340 | /// Lowers a `WValue` to the stack. This means when the `value` results in |
| 1343 | /// `.stack_offset` we calculate the pointer of this offset and use that. | 1341 | /// `.stack_offset` we calculate the pointer of this offset and use that. |
| 1344 | /// The value is left on the stack, and not stored in any temporary. | 1342 | /// The value is left on the stack, and not stored in any temporary. |
| 1345 | fn lowerToStack(self: *Self, value: WValue) !void { | 1343 | fn lowerToStack(func: *CodeGen, value: WValue) !void { |
| 1346 | switch (value) { | 1344 | switch (value) { |
| 1347 | .stack_offset => |offset| { | 1345 | .stack_offset => |offset| { |
| 1348 | try self.emitWValue(value); | 1346 | try func.emitWValue(value); |
| 1349 | if (offset.value > 0) { | 1347 | if (offset.value > 0) { |
| 1350 | switch (self.arch()) { | 1348 | switch (func.arch()) { |
| 1351 | .wasm32 => { | 1349 | .wasm32 => { |
| 1352 | try self.addImm32(@bitCast(i32, offset.value)); | 1350 | try func.addImm32(@bitCast(i32, offset.value)); |
| 1353 | try self.addTag(.i32_add); | 1351 | try func.addTag(.i32_add); |
| 1354 | }, | 1352 | }, |
| 1355 | .wasm64 => { | 1353 | .wasm64 => { |
| 1356 | try self.addImm64(offset.value); | 1354 | try func.addImm64(offset.value); |
| 1357 | try self.addTag(.i64_add); | 1355 | try func.addTag(.i64_add); |
| 1358 | }, | 1356 | }, |
| 1359 | else => unreachable, | 1357 | else => unreachable, |
| 1360 | } | 1358 | } |
| 1361 | } | 1359 | } |
| 1362 | }, | 1360 | }, |
| 1363 | else => try self.emitWValue(value), | 1361 | else => try func.emitWValue(value), |
| 1364 | } | 1362 | } |
| 1365 | } | 1363 | } |
| 1366 | | 1364 | |
| 1367 | /// Creates a local for the initial stack value | 1365 | /// Creates a local for the initial stack value |
| 1368 | /// Asserts `initial_stack_value` is `.none` | 1366 | /// Asserts `initial_stack_value` is `.none` |
| 1369 | fn initializeStack(self: *Self) !void { | 1367 | fn initializeStack(func: *CodeGen) !void { |
| 1370 | assert(self.initial_stack_value == .none); | 1368 | assert(func.initial_stack_value == .none); |
| 1371 | // Reserve a local to store the current stack pointer | 1369 | // Reserve a local to store the current stack pointer |
| 1372 | // We can later use this local to set the stack pointer back to the value | 1370 | // We can later use this local to set the stack pointer back to the value |
| 1373 | // we have stored here. | 1371 | // we have stored here. |
| 1374 | self.initial_stack_value = try self.ensureAllocLocal(Type.usize); | 1372 | func.initial_stack_value = try func.ensureAllocLocal(Type.usize); |
| 1375 | // Also reserve a local to store the bottom stack value | 1373 | // Also reserve a local to store the bottom stack value |
| 1376 | self.bottom_stack_value = try self.ensureAllocLocal(Type.usize); | 1374 | func.bottom_stack_value = try func.ensureAllocLocal(Type.usize); |
| 1377 | } | 1375 | } |
| 1378 | | 1376 | |
| 1379 | /// Reads the stack pointer from `Context.initial_stack_value` and writes it | 1377 | /// Reads the stack pointer from `Context.initial_stack_value` and writes it |
| 1380 | /// to the global stack pointer variable | 1378 | /// to the global stack pointer variable |
| 1381 | fn restoreStackPointer(self: *Self) !void { | 1379 | fn restoreStackPointer(func: *CodeGen) !void { |
| 1382 | // only restore the pointer if it was initialized | 1380 | // only restore the pointer if it was initialized |
| 1383 | if (self.initial_stack_value == .none) return; | 1381 | if (func.initial_stack_value == .none) return; |
| 1384 | // Get the original stack pointer's value | 1382 | // Get the original stack pointer's value |
| 1385 | try self.emitWValue(self.initial_stack_value); | 1383 | try func.emitWValue(func.initial_stack_value); |
| 1386 | | 1384 | |
| 1387 | // save its value in the global stack pointer | 1385 | // save its value in the global stack pointer |
| 1388 | try self.addLabel(.global_set, 0); | 1386 | try func.addLabel(.global_set, 0); |
| 1389 | } | 1387 | } |
| 1390 | | 1388 | |
| 1391 | /// From a given type, will create space on the virtual stack to store the value of such type. | 1389 | /// From a given type, will create space on the virtual stack to store the value of such type. |
| ... | @@ -1394,26 +1392,26 @@ fn restoreStackPointer(self: *Self) !void { | ... | @@ -1394,26 +1392,26 @@ fn restoreStackPointer(self: *Self) !void { |
| 1394 | /// moveStack unless a local was already created to store the pointer. | 1392 | /// moveStack unless a local was already created to store the pointer. |
| 1395 | /// | 1393 | /// |
| 1396 | /// Asserts Type has codegenbits | 1394 | /// Asserts Type has codegenbits |
| 1397 | fn allocStack(self: *Self, ty: Type) !WValue { | 1395 | fn allocStack(func: *CodeGen, ty: Type) !WValue { |
| 1398 | assert(ty.hasRuntimeBitsIgnoreComptime()); | 1396 | assert(ty.hasRuntimeBitsIgnoreComptime()); |
| 1399 | if (self.initial_stack_value == .none) { | 1397 | if (func.initial_stack_value == .none) { |
| 1400 | try self.initializeStack(); | 1398 | try func.initializeStack(); |
| 1401 | } | 1399 | } |
| 1402 | | 1400 | |
| 1403 | const abi_size = std.math.cast(u32, ty.abiSize(self.target)) orelse { | 1401 | const abi_size = std.math.cast(u32, ty.abiSize(func.target)) orelse { |
| 1404 | const module = self.bin_file.base.options.module.?; | 1402 | const module = func.bin_file.base.options.module.?; |
| 1405 | return self.fail("Type {} with ABI size of {d} exceeds stack frame size", .{ | 1403 | return func.fail("Type {} with ABI size of {d} exceeds stack frame size", .{ |
| 1406 | ty.fmt(module), ty.abiSize(self.target), | 1404 | ty.fmt(module), ty.abiSize(func.target), |
| 1407 | }); | 1405 | }); |
| 1408 | }; | 1406 | }; |
| 1409 | const abi_align = ty.abiAlignment(self.target); | 1407 | const abi_align = ty.abiAlignment(func.target); |
| 1410 | | 1408 | |
| 1411 | if (abi_align > self.stack_alignment) { | 1409 | if (abi_align > func.stack_alignment) { |
| 1412 | self.stack_alignment = abi_align; | 1410 | func.stack_alignment = abi_align; |
| 1413 | } | 1411 | } |
| 1414 | | 1412 | |
| 1415 | const offset = std.mem.alignForwardGeneric(u32, self.stack_size, abi_align); | 1413 | const offset = std.mem.alignForwardGeneric(u32, func.stack_size, abi_align); |
| 1416 | defer self.stack_size = offset + abi_size; | 1414 | defer func.stack_size = offset + abi_size; |
| 1417 | | 1415 | |
| 1418 | return WValue{ .stack_offset = .{ .value = offset, .references = 1 } }; | 1416 | return WValue{ .stack_offset = .{ .value = offset, .references = 1 } }; |
| 1419 | } | 1417 | } |
| ... | @@ -1422,31 +1420,31 @@ fn allocStack(self: *Self, ty: Type) !WValue { | ... | @@ -1422,31 +1420,31 @@ fn allocStack(self: *Self, ty: Type) !WValue { |
| 1422 | /// the value of its type will live. | 1420 | /// the value of its type will live. |
| 1423 | /// This is different from allocStack where this will use the pointer's alignment | 1421 | /// This is different from allocStack where this will use the pointer's alignment |
| 1424 | /// if it is set, to ensure the stack alignment will be set correctly. | 1422 | /// if it is set, to ensure the stack alignment will be set correctly. |
| 1425 | fn allocStackPtr(self: *Self, inst: Air.Inst.Index) !WValue { | 1423 | fn allocStackPtr(func: *CodeGen, inst: Air.Inst.Index) !WValue { |
| 1426 | const ptr_ty = self.air.typeOfIndex(inst); | 1424 | const ptr_ty = func.air.typeOfIndex(inst); |
| 1427 | const pointee_ty = ptr_ty.childType(); | 1425 | const pointee_ty = ptr_ty.childType(); |
| 1428 | | 1426 | |
| 1429 | if (self.initial_stack_value == .none) { | 1427 | if (func.initial_stack_value == .none) { |
| 1430 | try self.initializeStack(); | 1428 | try func.initializeStack(); |
| 1431 | } | 1429 | } |
| 1432 | | 1430 | |
| 1433 | if (!pointee_ty.hasRuntimeBitsIgnoreComptime()) { | 1431 | if (!pointee_ty.hasRuntimeBitsIgnoreComptime()) { |
| 1434 | return self.allocStack(Type.usize); // create a value containing just the stack pointer. | 1432 | return func.allocStack(Type.usize); // create a value containing just the stack pointer. |
| 1435 | } | 1433 | } |
| 1436 | | 1434 | |
| 1437 | const abi_alignment = ptr_ty.ptrAlignment(self.target); | 1435 | const abi_alignment = ptr_ty.ptrAlignment(func.target); |
| 1438 | const abi_size = std.math.cast(u32, pointee_ty.abiSize(self.target)) orelse { | 1436 | const abi_size = std.math.cast(u32, pointee_ty.abiSize(func.target)) orelse { |
| 1439 | const module = self.bin_file.base.options.module.?; | 1437 | const module = func.bin_file.base.options.module.?; |
| 1440 | return self.fail("Type {} with ABI size of {d} exceeds stack frame size", .{ | 1438 | return func.fail("Type {} with ABI size of {d} exceeds stack frame size", .{ |
| 1441 | pointee_ty.fmt(module), pointee_ty.abiSize(self.target), | 1439 | pointee_ty.fmt(module), pointee_ty.abiSize(func.target), |
| 1442 | }); | 1440 | }); |
| 1443 | }; | 1441 | }; |
| 1444 | if (abi_alignment > self.stack_alignment) { | 1442 | if (abi_alignment > func.stack_alignment) { |
| 1445 | self.stack_alignment = abi_alignment; | 1443 | func.stack_alignment = abi_alignment; |
| 1446 | } | 1444 | } |
| 1447 | | 1445 | |
| 1448 | const offset = std.mem.alignForwardGeneric(u32, self.stack_size, abi_alignment); | 1446 | const offset = std.mem.alignForwardGeneric(u32, func.stack_size, abi_alignment); |
| 1449 | defer self.stack_size = offset + abi_size; | 1447 | defer func.stack_size = offset + abi_size; |
| 1450 | | 1448 | |
| 1451 | return WValue{ .stack_offset = .{ .value = offset, .references = 1 } }; | 1449 | return WValue{ .stack_offset = .{ .value = offset, .references = 1 } }; |
| 1452 | } | 1450 | } |
| ... | @@ -1460,14 +1458,14 @@ fn toWasmBits(bits: u16) ?u16 { | ... | @@ -1460,14 +1458,14 @@ fn toWasmBits(bits: u16) ?u16 { |
| 1460 | | 1458 | |
| 1461 | /// Performs a copy of bytes for a given type. Copying all bytes | 1459 | /// Performs a copy of bytes for a given type. Copying all bytes |
| 1462 | /// from rhs to lhs. | 1460 | /// from rhs to lhs. |
| 1463 | fn memcpy(self: *Self, dst: WValue, src: WValue, len: WValue) !void { | 1461 | fn memcpy(func: *CodeGen, dst: WValue, src: WValue, len: WValue) !void { |
| 1464 | // When bulk_memory is enabled, we lower it to wasm's memcpy instruction. | 1462 | // When bulk_memory is enabled, we lower it to wasm's memcpy instruction. |
| 1465 | // If not, we lower it ourselves manually | 1463 | // If not, we lower it ourselves manually |
| 1466 | if (std.Target.wasm.featureSetHas(self.target.cpu.features, .bulk_memory)) { | 1464 | if (std.Target.wasm.featureSetHas(func.target.cpu.features, .bulk_memory)) { |
| 1467 | try self.lowerToStack(dst); | 1465 | try func.lowerToStack(dst); |
| 1468 | try self.lowerToStack(src); | 1466 | try func.lowerToStack(src); |
| 1469 | try self.emitWValue(len); | 1467 | try func.emitWValue(len); |
| 1470 | try self.addExtended(.memory_copy); | 1468 | try func.addExtended(.memory_copy); |
| 1471 | return; | 1469 | return; |
| 1472 | } | 1470 | } |
| 1473 | | 1471 | |
| ... | @@ -1485,17 +1483,17 @@ fn memcpy(self: *Self, dst: WValue, src: WValue, len: WValue) !void { | ... | @@ -1485,17 +1483,17 @@ fn memcpy(self: *Self, dst: WValue, src: WValue, len: WValue) !void { |
| 1485 | const rhs_base = src.offset(); | 1483 | const rhs_base = src.offset(); |
| 1486 | while (offset < length) : (offset += 1) { | 1484 | while (offset < length) : (offset += 1) { |
| 1487 | // get dst's address to store the result | 1485 | // get dst's address to store the result |
| 1488 | try self.emitWValue(dst); | 1486 | try func.emitWValue(dst); |
| 1489 | // load byte from src's address | 1487 | // load byte from src's address |
| 1490 | try self.emitWValue(src); | 1488 | try func.emitWValue(src); |
| 1491 | switch (self.arch()) { | 1489 | switch (func.arch()) { |
| 1492 | .wasm32 => { | 1490 | .wasm32 => { |
| 1493 | try self.addMemArg(.i32_load8_u, .{ .offset = rhs_base + offset, .alignment = 1 }); | 1491 | try func.addMemArg(.i32_load8_u, .{ .offset = rhs_base + offset, .alignment = 1 }); |
| 1494 | try self.addMemArg(.i32_store8, .{ .offset = lhs_base + offset, .alignment = 1 }); | 1492 | try func.addMemArg(.i32_store8, .{ .offset = lhs_base + offset, .alignment = 1 }); |
| 1495 | }, | 1493 | }, |
| 1496 | .wasm64 => { | 1494 | .wasm64 => { |
| 1497 | try self.addMemArg(.i64_load8_u, .{ .offset = rhs_base + offset, .alignment = 1 }); | 1495 | try func.addMemArg(.i64_load8_u, .{ .offset = rhs_base + offset, .alignment = 1 }); |
| 1498 | try self.addMemArg(.i64_store8, .{ .offset = lhs_base + offset, .alignment = 1 }); | 1496 | try func.addMemArg(.i64_store8, .{ .offset = lhs_base + offset, .alignment = 1 }); |
| 1499 | }, | 1497 | }, |
| 1500 | else => unreachable, | 1498 | else => unreachable, |
| 1501 | } | 1499 | } |
| ... | @@ -1504,50 +1502,50 @@ fn memcpy(self: *Self, dst: WValue, src: WValue, len: WValue) !void { | ... | @@ -1504,50 +1502,50 @@ fn memcpy(self: *Self, dst: WValue, src: WValue, len: WValue) !void { |
| 1504 | else => { | 1502 | else => { |
| 1505 | // TODO: We should probably lower this to a call to compiler_rt | 1503 | // TODO: We should probably lower this to a call to compiler_rt |
| 1506 | // But for now, we implement it manually | 1504 | // But for now, we implement it manually |
| 1507 | var offset = try self.ensureAllocLocal(Type.usize); // local for counter | 1505 | var offset = try func.ensureAllocLocal(Type.usize); // local for counter |
| 1508 | defer offset.free(self); | 1506 | defer offset.free(func); |
| 1509 | | 1507 | |
| 1510 | // outer block to jump to when loop is done | 1508 | // outer block to jump to when loop is done |
| 1511 | try self.startBlock(.block, wasm.block_empty); | 1509 | try func.startBlock(.block, wasm.block_empty); |
| 1512 | try self.startBlock(.loop, wasm.block_empty); | 1510 | try func.startBlock(.loop, wasm.block_empty); |
| 1513 | | 1511 | |
| 1514 | // loop condition (offset == length -> break) | 1512 | // loop condition (offset == length -> break) |
| 1515 | { | 1513 | { |
| 1516 | try self.emitWValue(offset); | 1514 | try func.emitWValue(offset); |
| 1517 | try self.emitWValue(len); | 1515 | try func.emitWValue(len); |
| 1518 | switch (self.arch()) { | 1516 | switch (func.arch()) { |
| 1519 | .wasm32 => try self.addTag(.i32_eq), | 1517 | .wasm32 => try func.addTag(.i32_eq), |
| 1520 | .wasm64 => try self.addTag(.i64_eq), | 1518 | .wasm64 => try func.addTag(.i64_eq), |
| 1521 | else => unreachable, | 1519 | else => unreachable, |
| 1522 | } | 1520 | } |
| 1523 | try self.addLabel(.br_if, 1); // jump out of loop into outer block (finished) | 1521 | try func.addLabel(.br_if, 1); // jump out of loop into outer block (finished) |
| 1524 | } | 1522 | } |
| 1525 | | 1523 | |
| 1526 | // get dst ptr | 1524 | // get dst ptr |
| 1527 | { | 1525 | { |
| 1528 | try self.emitWValue(dst); | 1526 | try func.emitWValue(dst); |
| 1529 | try self.emitWValue(offset); | 1527 | try func.emitWValue(offset); |
| 1530 | switch (self.arch()) { | 1528 | switch (func.arch()) { |
| 1531 | .wasm32 => try self.addTag(.i32_add), | 1529 | .wasm32 => try func.addTag(.i32_add), |
| 1532 | .wasm64 => try self.addTag(.i64_add), | 1530 | .wasm64 => try func.addTag(.i64_add), |
| 1533 | else => unreachable, | 1531 | else => unreachable, |
| 1534 | } | 1532 | } |
| 1535 | } | 1533 | } |
| 1536 | | 1534 | |
| 1537 | // get src value and also store in dst | 1535 | // get src value and also store in dst |
| 1538 | { | 1536 | { |
| 1539 | try self.emitWValue(src); | 1537 | try func.emitWValue(src); |
| 1540 | try self.emitWValue(offset); | 1538 | try func.emitWValue(offset); |
| 1541 | switch (self.arch()) { | 1539 | switch (func.arch()) { |
| 1542 | .wasm32 => { | 1540 | .wasm32 => { |
| 1543 | try self.addTag(.i32_add); | 1541 | try func.addTag(.i32_add); |
| 1544 | try self.addMemArg(.i32_load8_u, .{ .offset = src.offset(), .alignment = 1 }); | 1542 | try func.addMemArg(.i32_load8_u, .{ .offset = src.offset(), .alignment = 1 }); |
| 1545 | try self.addMemArg(.i32_store8, .{ .offset = dst.offset(), .alignment = 1 }); | 1543 | try func.addMemArg(.i32_store8, .{ .offset = dst.offset(), .alignment = 1 }); |
| 1546 | }, | 1544 | }, |
| 1547 | .wasm64 => { | 1545 | .wasm64 => { |
| 1548 | try self.addTag(.i64_add); | 1546 | try func.addTag(.i64_add); |
| 1549 | try self.addMemArg(.i64_load8_u, .{ .offset = src.offset(), .alignment = 1 }); | 1547 | try func.addMemArg(.i64_load8_u, .{ .offset = src.offset(), .alignment = 1 }); |
| 1550 | try self.addMemArg(.i64_store8, .{ .offset = dst.offset(), .alignment = 1 }); | 1548 | try func.addMemArg(.i64_store8, .{ .offset = dst.offset(), .alignment = 1 }); |
| 1551 | }, | 1549 | }, |
| 1552 | else => unreachable, | 1550 | else => unreachable, |
| 1553 | } | 1551 | } |
| ... | @@ -1555,33 +1553,33 @@ fn memcpy(self: *Self, dst: WValue, src: WValue, len: WValue) !void { | ... | @@ -1555,33 +1553,33 @@ fn memcpy(self: *Self, dst: WValue, src: WValue, len: WValue) !void { |
| 1555 | | 1553 | |
| 1556 | // increment loop counter | 1554 | // increment loop counter |
| 1557 | { | 1555 | { |
| 1558 | try self.emitWValue(offset); | 1556 | try func.emitWValue(offset); |
| 1559 | switch (self.arch()) { | 1557 | switch (func.arch()) { |
| 1560 | .wasm32 => { | 1558 | .wasm32 => { |
| 1561 | try self.addImm32(1); | 1559 | try func.addImm32(1); |
| 1562 | try self.addTag(.i32_add); | 1560 | try func.addTag(.i32_add); |
| 1563 | }, | 1561 | }, |
| 1564 | .wasm64 => { | 1562 | .wasm64 => { |
| 1565 | try self.addImm64(1); | 1563 | try func.addImm64(1); |
| 1566 | try self.addTag(.i64_add); | 1564 | try func.addTag(.i64_add); |
| 1567 | }, | 1565 | }, |
| 1568 | else => unreachable, | 1566 | else => unreachable, |
| 1569 | } | 1567 | } |
| 1570 | try self.addLabel(.local_set, offset.local.value); | 1568 | try func.addLabel(.local_set, offset.local.value); |
| 1571 | try self.addLabel(.br, 0); // jump to start of loop | 1569 | try func.addLabel(.br, 0); // jump to start of loop |
| 1572 | } | 1570 | } |
| 1573 | try self.endBlock(); // close off loop block | 1571 | try func.endBlock(); // close off loop block |
| 1574 | try self.endBlock(); // close off outer block | 1572 | try func.endBlock(); // close off outer block |
| 1575 | }, | 1573 | }, |
| 1576 | } | 1574 | } |
| 1577 | } | 1575 | } |
| 1578 | | 1576 | |
| 1579 | fn ptrSize(self: *const Self) u16 { | 1577 | fn ptrSize(func: *const CodeGen) u16 { |
| 1580 | return @divExact(self.target.cpu.arch.ptrBitWidth(), 8); | 1578 | return @divExact(func.target.cpu.arch.ptrBitWidth(), 8); |
| 1581 | } | 1579 | } |
| 1582 | | 1580 | |
| 1583 | fn arch(self: *const Self) std.Target.Cpu.Arch { | 1581 | fn arch(func: *const CodeGen) std.Target.Cpu.Arch { |
| 1584 | return self.target.cpu.arch; | 1582 | return func.target.cpu.arch; |
| 1585 | } | 1583 | } |
| 1586 | | 1584 | |
| 1587 | /// For a given `Type`, will return true when the type will be passed | 1585 | /// For a given `Type`, will return true when the type will be passed |
| ... | @@ -1639,191 +1637,191 @@ fn isByRef(ty: Type, target: std.Target) bool { | ... | @@ -1639,191 +1637,191 @@ fn isByRef(ty: Type, target: std.Target) bool { |
| 1639 | /// This can be used to get a pointer to a struct field, error payload, etc. | 1637 | /// This can be used to get a pointer to a struct field, error payload, etc. |
| 1640 | /// By providing `modify` as action, it will modify the given `ptr_value` instead of making a new | 1638 | /// By providing `modify` as action, it will modify the given `ptr_value` instead of making a new |
| 1641 | /// local value to store the pointer. This allows for local re-use and improves binary size. | 1639 | /// local value to store the pointer. This allows for local re-use and improves binary size. |
| 1642 | fn buildPointerOffset(self: *Self, ptr_value: WValue, offset: u64, action: enum { modify, new }) InnerError!WValue { | 1640 | fn buildPointerOffset(func: *CodeGen, ptr_value: WValue, offset: u64, action: enum { modify, new }) InnerError!WValue { |
| 1643 | // do not perform arithmetic when offset is 0. | 1641 | // do not perform arithmetic when offset is 0. |
| 1644 | if (offset == 0 and ptr_value.offset() == 0 and action == .modify) return ptr_value; | 1642 | if (offset == 0 and ptr_value.offset() == 0 and action == .modify) return ptr_value; |
| 1645 | const result_ptr: WValue = switch (action) { | 1643 | const result_ptr: WValue = switch (action) { |
| 1646 | .new => try self.ensureAllocLocal(Type.usize), | 1644 | .new => try func.ensureAllocLocal(Type.usize), |
| 1647 | .modify => ptr_value, | 1645 | .modify => ptr_value, |
| 1648 | }; | 1646 | }; |
| 1649 | try self.emitWValue(ptr_value); | 1647 | try func.emitWValue(ptr_value); |
| 1650 | if (offset + ptr_value.offset() > 0) { | 1648 | if (offset + ptr_value.offset() > 0) { |
| 1651 | switch (self.arch()) { | 1649 | switch (func.arch()) { |
| 1652 | .wasm32 => { | 1650 | .wasm32 => { |
| 1653 | try self.addImm32(@bitCast(i32, @intCast(u32, offset + ptr_value.offset()))); | 1651 | try func.addImm32(@bitCast(i32, @intCast(u32, offset + ptr_value.offset()))); |
| 1654 | try self.addTag(.i32_add); | 1652 | try func.addTag(.i32_add); |
| 1655 | }, | 1653 | }, |
| 1656 | .wasm64 => { | 1654 | .wasm64 => { |
| 1657 | try self.addImm64(offset + ptr_value.offset()); | 1655 | try func.addImm64(offset + ptr_value.offset()); |
| 1658 | try self.addTag(.i64_add); | 1656 | try func.addTag(.i64_add); |
| 1659 | }, | 1657 | }, |
| 1660 | else => unreachable, | 1658 | else => unreachable, |
| 1661 | } | 1659 | } |
| 1662 | } | 1660 | } |
| 1663 | try self.addLabel(.local_set, result_ptr.local.value); | 1661 | try func.addLabel(.local_set, result_ptr.local.value); |
| 1664 | return result_ptr; | 1662 | return result_ptr; |
| 1665 | } | 1663 | } |
| 1666 | | 1664 | |
| 1667 | fn genInst(self: *Self, inst: Air.Inst.Index) InnerError!void { | 1665 | fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 1668 | const air_tags = self.air.instructions.items(.tag); | 1666 | const air_tags = func.air.instructions.items(.tag); |
| 1669 | return switch (air_tags[inst]) { | 1667 | return switch (air_tags[inst]) { |
| 1670 | .constant => unreachable, | 1668 | .constant => unreachable, |
| 1671 | .const_ty => unreachable, | 1669 | .const_ty => unreachable, |
| 1672 | | 1670 | |
| 1673 | .add => self.airBinOp(inst, .add), | 1671 | .add => func.airBinOp(inst, .add), |
| 1674 | .add_sat => self.airSatBinOp(inst, .add), | 1672 | .add_sat => func.airSatBinOp(inst, .add), |
| 1675 | .addwrap => self.airWrapBinOp(inst, .add), | 1673 | .addwrap => func.airWrapBinOp(inst, .add), |
| 1676 | .sub => self.airBinOp(inst, .sub), | 1674 | .sub => func.airBinOp(inst, .sub), |
| 1677 | .sub_sat => self.airSatBinOp(inst, .sub), | 1675 | .sub_sat => func.airSatBinOp(inst, .sub), |
| 1678 | .subwrap => self.airWrapBinOp(inst, .sub), | 1676 | .subwrap => func.airWrapBinOp(inst, .sub), |
| 1679 | .mul => self.airBinOp(inst, .mul), | 1677 | .mul => func.airBinOp(inst, .mul), |
| 1680 | .mulwrap => self.airWrapBinOp(inst, .mul), | 1678 | .mulwrap => func.airWrapBinOp(inst, .mul), |
| 1681 | .div_float, | 1679 | .div_float, |
| 1682 | .div_exact, | 1680 | .div_exact, |
| 1683 | .div_trunc, | 1681 | .div_trunc, |
| 1684 | => self.airDiv(inst), | 1682 | => func.airDiv(inst), |
| 1685 | .div_floor => self.airDivFloor(inst), | 1683 | .div_floor => func.airDivFloor(inst), |
| 1686 | .ceil => self.airCeilFloorTrunc(inst, .ceil), | 1684 | .ceil => func.airCeilFloorTrunc(inst, .ceil), |
| 1687 | .floor => self.airCeilFloorTrunc(inst, .floor), | 1685 | .floor => func.airCeilFloorTrunc(inst, .floor), |
| 1688 | .trunc_float => self.airCeilFloorTrunc(inst, .trunc), | 1686 | .trunc_float => func.airCeilFloorTrunc(inst, .trunc), |
| 1689 | .bit_and => self.airBinOp(inst, .@"and"), | 1687 | .bit_and => func.airBinOp(inst, .@"and"), |
| 1690 | .bit_or => self.airBinOp(inst, .@"or"), | 1688 | .bit_or => func.airBinOp(inst, .@"or"), |
| 1691 | .bool_and => self.airBinOp(inst, .@"and"), | 1689 | .bool_and => func.airBinOp(inst, .@"and"), |
| 1692 | .bool_or => self.airBinOp(inst, .@"or"), | 1690 | .bool_or => func.airBinOp(inst, .@"or"), |
| 1693 | .rem => self.airBinOp(inst, .rem), | 1691 | .rem => func.airBinOp(inst, .rem), |
| 1694 | .shl => self.airWrapBinOp(inst, .shl), | 1692 | .shl => func.airWrapBinOp(inst, .shl), |
| 1695 | .shl_exact => self.airBinOp(inst, .shl), | 1693 | .shl_exact => func.airBinOp(inst, .shl), |
| 1696 | .shl_sat => self.airShlSat(inst), | 1694 | .shl_sat => func.airShlSat(inst), |
| 1697 | .shr, .shr_exact => self.airBinOp(inst, .shr), | 1695 | .shr, .shr_exact => func.airBinOp(inst, .shr), |
| 1698 | .xor => self.airBinOp(inst, .xor), | 1696 | .xor => func.airBinOp(inst, .xor), |
| 1699 | .max => self.airMaxMin(inst, .max), | 1697 | .max => func.airMaxMin(inst, .max), |
| 1700 | .min => self.airMaxMin(inst, .min), | 1698 | .min => func.airMaxMin(inst, .min), |
| 1701 | .mul_add => self.airMulAdd(inst), | 1699 | .mul_add => func.airMulAdd(inst), |
| 1702 | | 1700 | |
| 1703 | .add_with_overflow => self.airAddSubWithOverflow(inst, .add), | 1701 | .add_with_overflow => func.airAddSubWithOverflow(inst, .add), |
| 1704 | .sub_with_overflow => self.airAddSubWithOverflow(inst, .sub), | 1702 | .sub_with_overflow => func.airAddSubWithOverflow(inst, .sub), |
| 1705 | .shl_with_overflow => self.airShlWithOverflow(inst), | 1703 | .shl_with_overflow => func.airShlWithOverflow(inst), |
| 1706 | .mul_with_overflow => self.airMulWithOverflow(inst), | 1704 | .mul_with_overflow => func.airMulWithOverflow(inst), |
| 1707 | | 1705 | |
| 1708 | .clz => self.airClz(inst), | 1706 | .clz => func.airClz(inst), |
| 1709 | .ctz => self.airCtz(inst), | 1707 | .ctz => func.airCtz(inst), |
| 1710 | | 1708 | |
| 1711 | .cmp_eq => self.airCmp(inst, .eq), | 1709 | .cmp_eq => func.airCmp(inst, .eq), |
| 1712 | .cmp_gte => self.airCmp(inst, .gte), | 1710 | .cmp_gte => func.airCmp(inst, .gte), |
| 1713 | .cmp_gt => self.airCmp(inst, .gt), | 1711 | .cmp_gt => func.airCmp(inst, .gt), |
| 1714 | .cmp_lte => self.airCmp(inst, .lte), | 1712 | .cmp_lte => func.airCmp(inst, .lte), |
| 1715 | .cmp_lt => self.airCmp(inst, .lt), | 1713 | .cmp_lt => func.airCmp(inst, .lt), |
| 1716 | .cmp_neq => self.airCmp(inst, .neq), | 1714 | .cmp_neq => func.airCmp(inst, .neq), |
| 1717 | | 1715 | |
| 1718 | .cmp_vector => self.airCmpVector(inst), | 1716 | .cmp_vector => func.airCmpVector(inst), |
| 1719 | .cmp_lt_errors_len => self.airCmpLtErrorsLen(inst), | 1717 | .cmp_lt_errors_len => func.airCmpLtErrorsLen(inst), |
| 1720 | | 1718 | |
| 1721 | .array_elem_val => self.airArrayElemVal(inst), | 1719 | .array_elem_val => func.airArrayElemVal(inst), |
| 1722 | .array_to_slice => self.airArrayToSlice(inst), | 1720 | .array_to_slice => func.airArrayToSlice(inst), |
| 1723 | .alloc => self.airAlloc(inst), | 1721 | .alloc => func.airAlloc(inst), |
| 1724 | .arg => self.airArg(inst), | 1722 | .arg => func.airArg(inst), |
| 1725 | .bitcast => self.airBitcast(inst), | 1723 | .bitcast => func.airBitcast(inst), |
| 1726 | .block => self.airBlock(inst), | 1724 | .block => func.airBlock(inst), |
| 1727 | .breakpoint => self.airBreakpoint(inst), | 1725 | .breakpoint => func.airBreakpoint(inst), |
| 1728 | .br => self.airBr(inst), | 1726 | .br => func.airBr(inst), |
| 1729 | .bool_to_int => self.airBoolToInt(inst), | 1727 | .bool_to_int => func.airBoolToInt(inst), |
| 1730 | .cond_br => self.airCondBr(inst), | 1728 | .cond_br => func.airCondBr(inst), |
| 1731 | .intcast => self.airIntcast(inst), | 1729 | .intcast => func.airIntcast(inst), |
| 1732 | .fptrunc => self.airFptrunc(inst), | 1730 | .fptrunc => func.airFptrunc(inst), |
| 1733 | .fpext => self.airFpext(inst), | 1731 | .fpext => func.airFpext(inst), |
| 1734 | .float_to_int => self.airFloatToInt(inst), | 1732 | .float_to_int => func.airFloatToInt(inst), |
| 1735 | .int_to_float => self.airIntToFloat(inst), | 1733 | .int_to_float => func.airIntToFloat(inst), |
| 1736 | .get_union_tag => self.airGetUnionTag(inst), | 1734 | .get_union_tag => func.airGetUnionTag(inst), |
| 1737 | | 1735 | |
| 1738 | .@"try" => self.airTry(inst), | 1736 | .@"try" => func.airTry(inst), |
| 1739 | .try_ptr => self.airTryPtr(inst), | 1737 | .try_ptr => func.airTryPtr(inst), |
| 1740 | | 1738 | |
| 1741 | // TODO | 1739 | // TODO |
| 1742 | .dbg_inline_begin, | 1740 | .dbg_inline_begin, |
| 1743 | .dbg_inline_end, | 1741 | .dbg_inline_end, |
| 1744 | .dbg_block_begin, | 1742 | .dbg_block_begin, |
| 1745 | .dbg_block_end, | 1743 | .dbg_block_end, |
| 1746 | => self.finishAir(inst, .none, &.{}), | 1744 | => func.finishAir(inst, .none, &.{}), |
| 1747 | | 1745 | |
| 1748 | .dbg_var_ptr => self.airDbgVar(inst, true), | 1746 | .dbg_var_ptr => func.airDbgVar(inst, true), |
| 1749 | .dbg_var_val => self.airDbgVar(inst, false), | 1747 | .dbg_var_val => func.airDbgVar(inst, false), |
| 1750 | | 1748 | |
| 1751 | .dbg_stmt => self.airDbgStmt(inst), | 1749 | .dbg_stmt => func.airDbgStmt(inst), |
| 1752 | | 1750 | |
| 1753 | .call => self.airCall(inst, .auto), | 1751 | .call => func.airCall(inst, .auto), |
| 1754 | .call_always_tail => self.airCall(inst, .always_tail), | 1752 | .call_always_tail => func.airCall(inst, .always_tail), |
| 1755 | .call_never_tail => self.airCall(inst, .never_tail), | 1753 | .call_never_tail => func.airCall(inst, .never_tail), |
| 1756 | .call_never_inline => self.airCall(inst, .never_inline), | 1754 | .call_never_inline => func.airCall(inst, .never_inline), |
| 1757 | | 1755 | |
| 1758 | .is_err => self.airIsErr(inst, .i32_ne), | 1756 | .is_err => func.airIsErr(inst, .i32_ne), |
| 1759 | .is_non_err => self.airIsErr(inst, .i32_eq), | 1757 | .is_non_err => func.airIsErr(inst, .i32_eq), |
| 1760 | | 1758 | |
| 1761 | .is_null => self.airIsNull(inst, .i32_eq, .value), | 1759 | .is_null => func.airIsNull(inst, .i32_eq, .value), |
| 1762 | .is_non_null => self.airIsNull(inst, .i32_ne, .value), | 1760 | .is_non_null => func.airIsNull(inst, .i32_ne, .value), |
| 1763 | .is_null_ptr => self.airIsNull(inst, .i32_eq, .ptr), | 1761 | .is_null_ptr => func.airIsNull(inst, .i32_eq, .ptr), |
| 1764 | .is_non_null_ptr => self.airIsNull(inst, .i32_ne, .ptr), | 1762 | .is_non_null_ptr => func.airIsNull(inst, .i32_ne, .ptr), |
| 1765 | | 1763 | |
| 1766 | .load => self.airLoad(inst), | 1764 | .load => func.airLoad(inst), |
| 1767 | .loop => self.airLoop(inst), | 1765 | .loop => func.airLoop(inst), |
| 1768 | .memset => self.airMemset(inst), | 1766 | .memset => func.airMemset(inst), |
| 1769 | .not => self.airNot(inst), | 1767 | .not => func.airNot(inst), |
| 1770 | .optional_payload => self.airOptionalPayload(inst), | 1768 | .optional_payload => func.airOptionalPayload(inst), |
| 1771 | .optional_payload_ptr => self.airOptionalPayloadPtr(inst), | 1769 | .optional_payload_ptr => func.airOptionalPayloadPtr(inst), |
| 1772 | .optional_payload_ptr_set => self.airOptionalPayloadPtrSet(inst), | 1770 | .optional_payload_ptr_set => func.airOptionalPayloadPtrSet(inst), |
| 1773 | .ptr_add => self.airPtrBinOp(inst, .add), | 1771 | .ptr_add => func.airPtrBinOp(inst, .add), |
| 1774 | .ptr_sub => self.airPtrBinOp(inst, .sub), | 1772 | .ptr_sub => func.airPtrBinOp(inst, .sub), |
| 1775 | .ptr_elem_ptr => self.airPtrElemPtr(inst), | 1773 | .ptr_elem_ptr => func.airPtrElemPtr(inst), |
| 1776 | .ptr_elem_val => self.airPtrElemVal(inst), | 1774 | .ptr_elem_val => func.airPtrElemVal(inst), |
| 1777 | .ptrtoint => self.airPtrToInt(inst), | 1775 | .ptrtoint => func.airPtrToInt(inst), |
| 1778 | .ret => self.airRet(inst), | 1776 | .ret => func.airRet(inst), |
| 1779 | .ret_ptr => self.airRetPtr(inst), | 1777 | .ret_ptr => func.airRetPtr(inst), |
| 1780 | .ret_load => self.airRetLoad(inst), | 1778 | .ret_load => func.airRetLoad(inst), |
| 1781 | .splat => self.airSplat(inst), | 1779 | .splat => func.airSplat(inst), |
| 1782 | .select => self.airSelect(inst), | 1780 | .select => func.airSelect(inst), |
| 1783 | .shuffle => self.airShuffle(inst), | 1781 | .shuffle => func.airShuffle(inst), |
| 1784 | .reduce => self.airReduce(inst), | 1782 | .reduce => func.airReduce(inst), |
| 1785 | .aggregate_init => self.airAggregateInit(inst), | 1783 | .aggregate_init => func.airAggregateInit(inst), |
| 1786 | .union_init => self.airUnionInit(inst), | 1784 | .union_init => func.airUnionInit(inst), |
| 1787 | .prefetch => self.airPrefetch(inst), | 1785 | .prefetch => func.airPrefetch(inst), |
| 1788 | .popcount => self.airPopcount(inst), | 1786 | .popcount => func.airPopcount(inst), |
| 1789 | .byte_swap => self.airByteSwap(inst), | 1787 | .byte_swap => func.airByteSwap(inst), |
| 1790 | | 1788 | |
| 1791 | .slice => self.airSlice(inst), | 1789 | .slice => func.airSlice(inst), |
| 1792 | .slice_len => self.airSliceLen(inst), | 1790 | .slice_len => func.airSliceLen(inst), |
| 1793 | .slice_elem_val => self.airSliceElemVal(inst), | 1791 | .slice_elem_val => func.airSliceElemVal(inst), |
| 1794 | .slice_elem_ptr => self.airSliceElemPtr(inst), | 1792 | .slice_elem_ptr => func.airSliceElemPtr(inst), |
| 1795 | .slice_ptr => self.airSlicePtr(inst), | 1793 | .slice_ptr => func.airSlicePtr(inst), |
| 1796 | .ptr_slice_len_ptr => self.airPtrSliceFieldPtr(inst, self.ptrSize()), | 1794 | .ptr_slice_len_ptr => func.airPtrSliceFieldPtr(inst, func.ptrSize()), |
| 1797 | .ptr_slice_ptr_ptr => self.airPtrSliceFieldPtr(inst, 0), | 1795 | .ptr_slice_ptr_ptr => func.airPtrSliceFieldPtr(inst, 0), |
| 1798 | .store => self.airStore(inst), | 1796 | .store => func.airStore(inst), |
| 1799 | | 1797 | |
| 1800 | .set_union_tag => self.airSetUnionTag(inst), | 1798 | .set_union_tag => func.airSetUnionTag(inst), |
| 1801 | .struct_field_ptr => self.airStructFieldPtr(inst), | 1799 | .struct_field_ptr => func.airStructFieldPtr(inst), |
| 1802 | .struct_field_ptr_index_0 => self.airStructFieldPtrIndex(inst, 0), | 1800 | .struct_field_ptr_index_0 => func.airStructFieldPtrIndex(inst, 0), |
| 1803 | .struct_field_ptr_index_1 => self.airStructFieldPtrIndex(inst, 1), | 1801 | .struct_field_ptr_index_1 => func.airStructFieldPtrIndex(inst, 1), |
| 1804 | .struct_field_ptr_index_2 => self.airStructFieldPtrIndex(inst, 2), | 1802 | .struct_field_ptr_index_2 => func.airStructFieldPtrIndex(inst, 2), |
| 1805 | .struct_field_ptr_index_3 => self.airStructFieldPtrIndex(inst, 3), | 1803 | .struct_field_ptr_index_3 => func.airStructFieldPtrIndex(inst, 3), |
| 1806 | .struct_field_val => self.airStructFieldVal(inst), | 1804 | .struct_field_val => func.airStructFieldVal(inst), |
| 1807 | .field_parent_ptr => self.airFieldParentPtr(inst), | 1805 | .field_parent_ptr => func.airFieldParentPtr(inst), |
| 1808 | | 1806 | |
| 1809 | .switch_br => self.airSwitchBr(inst), | 1807 | .switch_br => func.airSwitchBr(inst), |
| 1810 | .trunc => self.airTrunc(inst), | 1808 | .trunc => func.airTrunc(inst), |
| 1811 | .unreach => self.airUnreachable(inst), | 1809 | .unreach => func.airUnreachable(inst), |
| 1812 | | 1810 | |
| 1813 | .wrap_optional => self.airWrapOptional(inst), | 1811 | .wrap_optional => func.airWrapOptional(inst), |
| 1814 | .unwrap_errunion_payload => self.airUnwrapErrUnionPayload(inst, false), | 1812 | .unwrap_errunion_payload => func.airUnwrapErrUnionPayload(inst, false), |
| 1815 | .unwrap_errunion_payload_ptr => self.airUnwrapErrUnionPayload(inst, true), | 1813 | .unwrap_errunion_payload_ptr => func.airUnwrapErrUnionPayload(inst, true), |
| 1816 | .unwrap_errunion_err => self.airUnwrapErrUnionError(inst, false), | 1814 | .unwrap_errunion_err => func.airUnwrapErrUnionError(inst, false), |
| 1817 | .unwrap_errunion_err_ptr => self.airUnwrapErrUnionError(inst, true), | 1815 | .unwrap_errunion_err_ptr => func.airUnwrapErrUnionError(inst, true), |
| 1818 | .wrap_errunion_payload => self.airWrapErrUnionPayload(inst), | 1816 | .wrap_errunion_payload => func.airWrapErrUnionPayload(inst), |
| 1819 | .wrap_errunion_err => self.airWrapErrUnionErr(inst), | 1817 | .wrap_errunion_err => func.airWrapErrUnionErr(inst), |
| 1820 | .errunion_payload_ptr_set => self.airErrUnionPayloadPtrSet(inst), | 1818 | .errunion_payload_ptr_set => func.airErrUnionPayloadPtrSet(inst), |
| 1821 | .error_name => self.airErrorName(inst), | 1819 | .error_name => func.airErrorName(inst), |
| 1822 | | 1820 | |
| 1823 | .wasm_memory_size => self.airWasmMemorySize(inst), | 1821 | .wasm_memory_size => func.airWasmMemorySize(inst), |
| 1824 | .wasm_memory_grow => self.airWasmMemoryGrow(inst), | 1822 | .wasm_memory_grow => func.airWasmMemoryGrow(inst), |
| 1825 | | 1823 | |
| 1826 | .memcpy => self.airMemcpy(inst), | 1824 | .memcpy => func.airMemcpy(inst), |
| 1827 | | 1825 | |
| 1828 | .mul_sat, | 1826 | .mul_sat, |
| 1829 | .mod, | 1827 | .mod, |
| ... | @@ -1862,7 +1860,7 @@ fn genInst(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -1862,7 +1860,7 @@ fn genInst(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 1862 | .is_named_enum_value, | 1860 | .is_named_enum_value, |
| 1863 | .error_set_has_value, | 1861 | .error_set_has_value, |
| 1864 | .addrspace_cast, | 1862 | .addrspace_cast, |
| 1865 | => |tag| return self.fail("TODO: Implement wasm inst: {s}", .{@tagName(tag)}), | 1863 | => |tag| return func.fail("TODO: Implement wasm inst: {s}", .{@tagName(tag)}), |
| 1866 | | 1864 | |
| 1867 | .add_optimized, | 1865 | .add_optimized, |
| 1868 | .addwrap_optimized, | 1866 | .addwrap_optimized, |
| ... | @@ -1886,116 +1884,116 @@ fn genInst(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -1886,116 +1884,116 @@ fn genInst(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 1886 | .cmp_vector_optimized, | 1884 | .cmp_vector_optimized, |
| 1887 | .reduce_optimized, | 1885 | .reduce_optimized, |
| 1888 | .float_to_int_optimized, | 1886 | .float_to_int_optimized, |
| 1889 | => return self.fail("TODO implement optimized float mode", .{}), | 1887 | => return func.fail("TODO implement optimized float mode", .{}), |
| 1890 | }; | 1888 | }; |
| 1891 | } | 1889 | } |
| 1892 | | 1890 | |
| 1893 | fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { | 1891 | fn genBody(func: *CodeGen, body: []const Air.Inst.Index) InnerError!void { |
| 1894 | for (body) |inst| { | 1892 | for (body) |inst| { |
| 1895 | const old_bookkeeping_value = self.air_bookkeeping; | 1893 | const old_bookkeeping_value = func.air_bookkeeping; |
| 1896 | // TODO: Determine why we need to pre-allocate an extra 4 possible values here. | 1894 | // TODO: Determine why we need to pre-allocate an extra 4 possible values here. |
| 1897 | try self.currentBranch().values.ensureUnusedCapacity(self.gpa, Liveness.bpi + 4); | 1895 | try func.currentBranch().values.ensureUnusedCapacity(func.gpa, Liveness.bpi + 4); |
| 1898 | try self.genInst(inst); | 1896 | try func.genInst(inst); |
| 1899 | | 1897 | |
| 1900 | if (builtin.mode == .Debug and self.air_bookkeeping < old_bookkeeping_value + 1) { | 1898 | if (builtin.mode == .Debug and func.air_bookkeeping < old_bookkeeping_value + 1) { |
| 1901 | std.debug.panic("Missing call to `finishAir` in AIR instruction %{d} ('{}')", .{ | 1899 | std.debug.panic("Missing call to `finishAir` in AIR instruction %{d} ('{}')", .{ |
| 1902 | inst, | 1900 | inst, |
| 1903 | self.air.instructions.items(.tag)[inst], | 1901 | func.air.instructions.items(.tag)[inst], |
| 1904 | }); | 1902 | }); |
| 1905 | } | 1903 | } |
| 1906 | } | 1904 | } |
| 1907 | } | 1905 | } |
| 1908 | | 1906 | |
| 1909 | fn airRet(self: *Self, inst: Air.Inst.Index) InnerError!void { | 1907 | fn airRet(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 1910 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 1908 | const un_op = func.air.instructions.items(.data)[inst].un_op; |
| 1911 | const operand = try self.resolveInst(un_op); | 1909 | const operand = try func.resolveInst(un_op); |
| 1912 | const fn_info = self.decl.ty.fnInfo(); | 1910 | const fn_info = func.decl.ty.fnInfo(); |
| 1913 | const ret_ty = fn_info.return_type; | 1911 | const ret_ty = fn_info.return_type; |
| 1914 | | 1912 | |
| 1915 | // result must be stored in the stack and we return a pointer | 1913 | // result must be stored in the stack and we return a pointer |
| 1916 | // to the stack instead | 1914 | // to the stack instead |
| 1917 | if (self.return_value != .none) { | 1915 | if (func.return_value != .none) { |
| 1918 | try self.store(self.return_value, operand, ret_ty, 0); | 1916 | try func.store(func.return_value, operand, ret_ty, 0); |
| 1919 | } else if (fn_info.cc == .C and ret_ty.hasRuntimeBitsIgnoreComptime()) { | 1917 | } else if (fn_info.cc == .C and ret_ty.hasRuntimeBitsIgnoreComptime()) { |
| 1920 | switch (ret_ty.zigTypeTag()) { | 1918 | switch (ret_ty.zigTypeTag()) { |
| 1921 | // Aggregate types can be lowered as a singular value | 1919 | // Aggregate types can be lowered as a singular value |
| 1922 | .Struct, .Union => { | 1920 | .Struct, .Union => { |
| 1923 | const scalar_type = abi.scalarType(ret_ty, self.target); | 1921 | const scalar_type = abi.scalarType(ret_ty, func.target); |
| 1924 | try self.emitWValue(operand); | 1922 | try func.emitWValue(operand); |
| 1925 | const opcode = buildOpcode(.{ | 1923 | const opcode = buildOpcode(.{ |
| 1926 | .op = .load, | 1924 | .op = .load, |
| 1927 | .width = @intCast(u8, scalar_type.abiSize(self.target) * 8), | 1925 | .width = @intCast(u8, scalar_type.abiSize(func.target) * 8), |
| 1928 | .signedness = if (scalar_type.isSignedInt()) .signed else .unsigned, | 1926 | .signedness = if (scalar_type.isSignedInt()) .signed else .unsigned, |
| 1929 | .valtype1 = typeToValtype(scalar_type, self.target), | 1927 | .valtype1 = typeToValtype(scalar_type, func.target), |
| 1930 | }); | 1928 | }); |
| 1931 | try self.addMemArg(Mir.Inst.Tag.fromOpcode(opcode), .{ | 1929 | try func.addMemArg(Mir.Inst.Tag.fromOpcode(opcode), .{ |
| 1932 | .offset = operand.offset(), | 1930 | .offset = operand.offset(), |
| 1933 | .alignment = scalar_type.abiAlignment(self.target), | 1931 | .alignment = scalar_type.abiAlignment(func.target), |
| 1934 | }); | 1932 | }); |
| 1935 | }, | 1933 | }, |
| 1936 | else => try self.emitWValue(operand), | 1934 | else => try func.emitWValue(operand), |
| 1937 | } | 1935 | } |
| 1938 | } else { | 1936 | } else { |
| 1939 | if (!ret_ty.hasRuntimeBitsIgnoreComptime() and ret_ty.isError()) { | 1937 | if (!ret_ty.hasRuntimeBitsIgnoreComptime() and ret_ty.isError()) { |
| 1940 | try self.addImm32(0); | 1938 | try func.addImm32(0); |
| 1941 | } else { | 1939 | } else { |
| 1942 | try self.emitWValue(operand); | 1940 | try func.emitWValue(operand); |
| 1943 | } | 1941 | } |
| 1944 | } | 1942 | } |
| 1945 | try self.restoreStackPointer(); | 1943 | try func.restoreStackPointer(); |
| 1946 | try self.addTag(.@"return"); | 1944 | try func.addTag(.@"return"); |
| 1947 | | 1945 | |
| 1948 | self.finishAir(inst, .none, &.{un_op}); | 1946 | func.finishAir(inst, .none, &.{un_op}); |
| 1949 | } | 1947 | } |
| 1950 | | 1948 | |
| 1951 | fn airRetPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { | 1949 | fn airRetPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 1952 | const child_type = self.air.typeOfIndex(inst).childType(); | 1950 | const child_type = func.air.typeOfIndex(inst).childType(); |
| 1953 | | 1951 | |
| 1954 | var result = result: { | 1952 | var result = result: { |
| 1955 | if (!child_type.isFnOrHasRuntimeBitsIgnoreComptime()) { | 1953 | if (!child_type.isFnOrHasRuntimeBitsIgnoreComptime()) { |
| 1956 | break :result try self.allocStack(Type.usize); // create pointer to void | 1954 | break :result try func.allocStack(Type.usize); // create pointer to void |
| 1957 | } | 1955 | } |
| 1958 | | 1956 | |
| 1959 | const fn_info = self.decl.ty.fnInfo(); | 1957 | const fn_info = func.decl.ty.fnInfo(); |
| 1960 | if (firstParamSRet(fn_info.cc, fn_info.return_type, self.target)) { | 1958 | if (firstParamSRet(fn_info.cc, fn_info.return_type, func.target)) { |
| 1961 | break :result self.return_value; | 1959 | break :result func.return_value; |
| 1962 | } | 1960 | } |
| 1963 | | 1961 | |
| 1964 | break :result try self.allocStackPtr(inst); | 1962 | break :result try func.allocStackPtr(inst); |
| 1965 | }; | 1963 | }; |
| 1966 | | 1964 | |
| 1967 | self.finishAir(inst, result, &.{}); | 1965 | func.finishAir(inst, result, &.{}); |
| 1968 | } | 1966 | } |
| 1969 | | 1967 | |
| 1970 | fn airRetLoad(self: *Self, inst: Air.Inst.Index) InnerError!void { | 1968 | fn airRetLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 1971 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 1969 | const un_op = func.air.instructions.items(.data)[inst].un_op; |
| 1972 | const operand = try self.resolveInst(un_op); | 1970 | const operand = try func.resolveInst(un_op); |
| 1973 | const ret_ty = self.air.typeOf(un_op).childType(); | 1971 | const ret_ty = func.air.typeOf(un_op).childType(); |
| 1974 | if (!ret_ty.hasRuntimeBitsIgnoreComptime()) { | 1972 | if (!ret_ty.hasRuntimeBitsIgnoreComptime()) { |
| 1975 | if (ret_ty.isError()) { | 1973 | if (ret_ty.isError()) { |
| 1976 | try self.addImm32(0); | 1974 | try func.addImm32(0); |
| 1977 | } else { | 1975 | } else { |
| 1978 | return self.finishAir(inst, .none, &.{}); | 1976 | return func.finishAir(inst, .none, &.{}); |
| 1979 | } | 1977 | } |
| 1980 | } | 1978 | } |
| 1981 | | 1979 | |
| 1982 | const fn_info = self.decl.ty.fnInfo(); | 1980 | const fn_info = func.decl.ty.fnInfo(); |
| 1983 | if (!firstParamSRet(fn_info.cc, fn_info.return_type, self.target)) { | 1981 | if (!firstParamSRet(fn_info.cc, fn_info.return_type, func.target)) { |
| 1984 | // leave on the stack | 1982 | // leave on the stack |
| 1985 | _ = try self.load(operand, ret_ty, 0); | 1983 | _ = try func.load(operand, ret_ty, 0); |
| 1986 | } | 1984 | } |
| 1987 | | 1985 | |
| 1988 | try self.restoreStackPointer(); | 1986 | try func.restoreStackPointer(); |
| 1989 | try self.addTag(.@"return"); | 1987 | try func.addTag(.@"return"); |
| 1990 | return self.finishAir(inst, .none, &.{}); | 1988 | return func.finishAir(inst, .none, &.{}); |
| 1991 | } | 1989 | } |
| 1992 | | 1990 | |
| 1993 | fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.Modifier) InnerError!void { | 1991 | fn airCall(func: *CodeGen, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.Modifier) InnerError!void { |
| 1994 | if (modifier == .always_tail) return self.fail("TODO implement tail calls for wasm", .{}); | 1992 | if (modifier == .always_tail) return func.fail("TODO implement tail calls for wasm", .{}); |
| 1995 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 1993 | const pl_op = func.air.instructions.items(.data)[inst].pl_op; |
| 1996 | const extra = self.air.extraData(Air.Call, pl_op.payload); | 1994 | const extra = func.air.extraData(Air.Call, pl_op.payload); |
| 1997 | const args = @ptrCast([]const Air.Inst.Ref, self.air.extra[extra.end..][0..extra.data.args_len]); | 1995 | const args = @ptrCast([]const Air.Inst.Ref, func.air.extra[extra.end..][0..extra.data.args_len]); |
| 1998 | const ty = self.air.typeOf(pl_op.operand); | 1996 | const ty = func.air.typeOf(pl_op.operand); |
| 1999 | | 1997 | |
| 2000 | const fn_ty = switch (ty.zigTypeTag()) { | 1998 | const fn_ty = switch (ty.zigTypeTag()) { |
| 2001 | .Fn => ty, | 1999 | .Fn => ty, |
| ... | @@ -2004,21 +2002,21 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. | ... | @@ -2004,21 +2002,21 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. |
| 2004 | }; | 2002 | }; |
| 2005 | const ret_ty = fn_ty.fnReturnType(); | 2003 | const ret_ty = fn_ty.fnReturnType(); |
| 2006 | const fn_info = fn_ty.fnInfo(); | 2004 | const fn_info = fn_ty.fnInfo(); |
| 2007 | const first_param_sret = firstParamSRet(fn_info.cc, fn_info.return_type, self.target); | 2005 | const first_param_sret = firstParamSRet(fn_info.cc, fn_info.return_type, func.target); |
| 2008 | | 2006 | |
| 2009 | const callee: ?*Decl = blk: { | 2007 | const callee: ?*Decl = blk: { |
| 2010 | const func_val = self.air.value(pl_op.operand) orelse break :blk null; | 2008 | const func_val = func.air.value(pl_op.operand) orelse break :blk null; |
| 2011 | const module = self.bin_file.base.options.module.?; | 2009 | const module = func.bin_file.base.options.module.?; |
| 2012 | | 2010 | |
| 2013 | if (func_val.castTag(.function)) |func| { | 2011 | if (func_val.castTag(.function)) |function| { |
| 2014 | break :blk module.declPtr(func.data.owner_decl); | 2012 | break :blk module.declPtr(function.data.owner_decl); |
| 2015 | } else if (func_val.castTag(.extern_fn)) |extern_fn| { | 2013 | } else if (func_val.castTag(.extern_fn)) |extern_fn| { |
| 2016 | const ext_decl = module.declPtr(extern_fn.data.owner_decl); | 2014 | const ext_decl = module.declPtr(extern_fn.data.owner_decl); |
| 2017 | const ext_info = ext_decl.ty.fnInfo(); | 2015 | const ext_info = ext_decl.ty.fnInfo(); |
| 2018 | var func_type = try genFunctype(self.gpa, ext_info.cc, ext_info.param_types, ext_info.return_type, self.target); | 2016 | var func_type = try genFunctype(func.gpa, ext_info.cc, ext_info.param_types, ext_info.return_type, func.target); |
| 2019 | defer func_type.deinit(self.gpa); | 2017 | defer func_type.deinit(func.gpa); |
| 2020 | ext_decl.fn_link.wasm.type_index = try self.bin_file.putOrGetFuncType(func_type); | 2018 | ext_decl.fn_link.wasm.type_index = try func.bin_file.putOrGetFuncType(func_type); |
| 2021 | try self.bin_file.addOrUpdateImport( | 2019 | try func.bin_file.addOrUpdateImport( |
| 2022 | mem.sliceTo(ext_decl.name, 0), | 2020 | mem.sliceTo(ext_decl.name, 0), |
| 2023 | ext_decl.link.wasm.sym_index, | 2021 | ext_decl.link.wasm.sym_index, |
| 2024 | ext_decl.getExternFn().?.lib_name, | 2022 | ext_decl.getExternFn().?.lib_name, |
| ... | @@ -2028,151 +2026,151 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. | ... | @@ -2028,151 +2026,151 @@ fn airCall(self: *Self, inst: Air.Inst.Index, modifier: std.builtin.CallOptions. |
| 2028 | } else if (func_val.castTag(.decl_ref)) |decl_ref| { | 2026 | } else if (func_val.castTag(.decl_ref)) |decl_ref| { |
| 2029 | break :blk module.declPtr(decl_ref.data); | 2027 | break :blk module.declPtr(decl_ref.data); |
| 2030 | } | 2028 | } |
| 2031 | return self.fail("Expected a function, but instead found type '{}'", .{func_val.tag()}); | 2029 | return func.fail("Expected a function, but instead found type '{}'", .{func_val.tag()}); |
| 2032 | }; | 2030 | }; |
| 2033 | | 2031 | |
| 2034 | const sret = if (first_param_sret) blk: { | 2032 | const sret = if (first_param_sret) blk: { |
| 2035 | const sret_local = try self.allocStack(ret_ty); | 2033 | const sret_local = try func.allocStack(ret_ty); |
| 2036 | try self.lowerToStack(sret_local); | 2034 | try func.lowerToStack(sret_local); |
| 2037 | break :blk sret_local; | 2035 | break :blk sret_local; |
| 2038 | } else WValue{ .none = {} }; | 2036 | } else WValue{ .none = {} }; |
| 2039 | | 2037 | |
| 2040 | for (args) |arg| { | 2038 | for (args) |arg| { |
| 2041 | const arg_val = try self.resolveInst(arg); | 2039 | const arg_val = try func.resolveInst(arg); |
| 2042 | | 2040 | |
| 2043 | const arg_ty = self.air.typeOf(arg); | 2041 | const arg_ty = func.air.typeOf(arg); |
| 2044 | if (!arg_ty.hasRuntimeBitsIgnoreComptime()) continue; | 2042 | if (!arg_ty.hasRuntimeBitsIgnoreComptime()) continue; |
| 2045 | | 2043 | |
| 2046 | try self.lowerArg(fn_ty.fnInfo().cc, arg_ty, arg_val); | 2044 | try func.lowerArg(fn_ty.fnInfo().cc, arg_ty, arg_val); |
| 2047 | } | 2045 | } |
| 2048 | | 2046 | |
| 2049 | if (callee) |direct| { | 2047 | if (callee) |direct| { |
| 2050 | try self.addLabel(.call, direct.link.wasm.sym_index); | 2048 | try func.addLabel(.call, direct.link.wasm.sym_index); |
| 2051 | } else { | 2049 | } else { |
| 2052 | // in this case we call a function pointer | 2050 | // in this case we call a function pointer |
| 2053 | // so load its value onto the stack | 2051 | // so load its value onto the stack |
| 2054 | std.debug.assert(ty.zigTypeTag() == .Pointer); | 2052 | std.debug.assert(ty.zigTypeTag() == .Pointer); |
| 2055 | const operand = try self.resolveInst(pl_op.operand); | 2053 | const operand = try func.resolveInst(pl_op.operand); |
| 2056 | try self.emitWValue(operand); | 2054 | try func.emitWValue(operand); |
| 2057 | | 2055 | |
| 2058 | var fn_type = try genFunctype(self.gpa, fn_info.cc, fn_info.param_types, fn_info.return_type, self.target); | 2056 | var fn_type = try genFunctype(func.gpa, fn_info.cc, fn_info.param_types, fn_info.return_type, func.target); |
| 2059 | defer fn_type.deinit(self.gpa); | 2057 | defer fn_type.deinit(func.gpa); |
| 2060 | | 2058 | |
| 2061 | const fn_type_index = try self.bin_file.putOrGetFuncType(fn_type); | 2059 | const fn_type_index = try func.bin_file.putOrGetFuncType(fn_type); |
| 2062 | try self.addLabel(.call_indirect, fn_type_index); | 2060 | try func.addLabel(.call_indirect, fn_type_index); |
| 2063 | } | 2061 | } |
| 2064 | | 2062 | |
| 2065 | const result_value = result_value: { | 2063 | const result_value = result_value: { |
| 2066 | if (self.liveness.isUnused(inst) or (!ret_ty.hasRuntimeBitsIgnoreComptime() and !ret_ty.isError())) { | 2064 | if (func.liveness.isUnused(inst) or (!ret_ty.hasRuntimeBitsIgnoreComptime() and !ret_ty.isError())) { |
| 2067 | break :result_value WValue{ .none = {} }; | 2065 | break :result_value WValue{ .none = {} }; |
| 2068 | } else if (ret_ty.isNoReturn()) { | 2066 | } else if (ret_ty.isNoReturn()) { |
| 2069 | try self.addTag(.@"unreachable"); | 2067 | try func.addTag(.@"unreachable"); |
| 2070 | break :result_value WValue{ .none = {} }; | 2068 | break :result_value WValue{ .none = {} }; |
| 2071 | } else if (first_param_sret) { | 2069 | } else if (first_param_sret) { |
| 2072 | break :result_value sret; | 2070 | break :result_value sret; |
| 2073 | // TODO: Make this less fragile and optimize | 2071 | // TODO: Make this less fragile and optimize |
| 2074 | } else if (fn_ty.fnInfo().cc == .C and ret_ty.zigTypeTag() == .Struct or ret_ty.zigTypeTag() == .Union) { | 2072 | } else if (fn_ty.fnInfo().cc == .C and ret_ty.zigTypeTag() == .Struct or ret_ty.zigTypeTag() == .Union) { |
| 2075 | const result_local = try self.allocLocal(ret_ty); | 2073 | const result_local = try func.allocLocal(ret_ty); |
| 2076 | try self.addLabel(.local_set, result_local.local.value); | 2074 | try func.addLabel(.local_set, result_local.local.value); |
| 2077 | const scalar_type = abi.scalarType(ret_ty, self.target); | 2075 | const scalar_type = abi.scalarType(ret_ty, func.target); |
| 2078 | const result = try self.allocStack(scalar_type); | 2076 | const result = try func.allocStack(scalar_type); |
| 2079 | try self.store(result, result_local, scalar_type, 0); | 2077 | try func.store(result, result_local, scalar_type, 0); |
| 2080 | break :result_value result; | 2078 | break :result_value result; |
| 2081 | } else { | 2079 | } else { |
| 2082 | const result_local = try self.allocLocal(ret_ty); | 2080 | const result_local = try func.allocLocal(ret_ty); |
| 2083 | try self.addLabel(.local_set, result_local.local.value); | 2081 | try func.addLabel(.local_set, result_local.local.value); |
| 2084 | break :result_value result_local; | 2082 | break :result_value result_local; |
| 2085 | } | 2083 | } |
| 2086 | }; | 2084 | }; |
| 2087 | | 2085 | |
| 2088 | var bt = try self.iterateBigTomb(inst, 1 + args.len); | 2086 | var bt = try func.iterateBigTomb(inst, 1 + args.len); |
| 2089 | bt.feed(pl_op.operand); | 2087 | bt.feed(pl_op.operand); |
| 2090 | for (args) |arg| bt.feed(arg); | 2088 | for (args) |arg| bt.feed(arg); |
| 2091 | return bt.finishAir(result_value); | 2089 | return bt.finishAir(result_value); |
| 2092 | } | 2090 | } |
| 2093 | | 2091 | |
| 2094 | fn airAlloc(self: *Self, inst: Air.Inst.Index) InnerError!void { | 2092 | fn airAlloc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2095 | const value = try self.allocStackPtr(inst); | 2093 | const value = try func.allocStackPtr(inst); |
| 2096 | self.finishAir(inst, value, &.{}); | 2094 | func.finishAir(inst, value, &.{}); |
| 2097 | } | 2095 | } |
| 2098 | | 2096 | |
| 2099 | fn airStore(self: *Self, inst: Air.Inst.Index) InnerError!void { | 2097 | fn airStore(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2100 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 2098 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 2101 | | 2099 | |
| 2102 | const lhs = try self.resolveInst(bin_op.lhs); | 2100 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2103 | const rhs = try self.resolveInst(bin_op.rhs); | 2101 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2104 | const ty = self.air.typeOf(bin_op.lhs).childType(); | 2102 | const ty = func.air.typeOf(bin_op.lhs).childType(); |
| 2105 | | 2103 | |
| 2106 | try self.store(lhs, rhs, ty, 0); | 2104 | try func.store(lhs, rhs, ty, 0); |
| 2107 | self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 2105 | func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 2108 | } | 2106 | } |
| 2109 | | 2107 | |
| 2110 | fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerError!void { | 2108 | fn store(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerError!void { |
| 2111 | assert(!(lhs != .stack and rhs == .stack)); | 2109 | assert(!(lhs != .stack and rhs == .stack)); |
| 2112 | switch (ty.zigTypeTag()) { | 2110 | switch (ty.zigTypeTag()) { |
| 2113 | .ErrorUnion => { | 2111 | .ErrorUnion => { |
| 2114 | const pl_ty = ty.errorUnionPayload(); | 2112 | const pl_ty = ty.errorUnionPayload(); |
| 2115 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { | 2113 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { |
| 2116 | return self.store(lhs, rhs, Type.anyerror, 0); | 2114 | return func.store(lhs, rhs, Type.anyerror, 0); |
| 2117 | } | 2115 | } |
| 2118 | | 2116 | |
| 2119 | const len = @intCast(u32, ty.abiSize(self.target)); | 2117 | const len = @intCast(u32, ty.abiSize(func.target)); |
| 2120 | return self.memcpy(lhs, rhs, .{ .imm32 = len }); | 2118 | return func.memcpy(lhs, rhs, .{ .imm32 = len }); |
| 2121 | }, | 2119 | }, |
| 2122 | .Optional => { | 2120 | .Optional => { |
| 2123 | if (ty.isPtrLikeOptional()) { | 2121 | if (ty.isPtrLikeOptional()) { |
| 2124 | return self.store(lhs, rhs, Type.usize, 0); | 2122 | return func.store(lhs, rhs, Type.usize, 0); |
| 2125 | } | 2123 | } |
| 2126 | var buf: Type.Payload.ElemType = undefined; | 2124 | var buf: Type.Payload.ElemType = undefined; |
| 2127 | const pl_ty = ty.optionalChild(&buf); | 2125 | const pl_ty = ty.optionalChild(&buf); |
| 2128 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { | 2126 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { |
| 2129 | return self.store(lhs, rhs, Type.u8, 0); | 2127 | return func.store(lhs, rhs, Type.u8, 0); |
| 2130 | } | 2128 | } |
| 2131 | if (pl_ty.zigTypeTag() == .ErrorSet) { | 2129 | if (pl_ty.zigTypeTag() == .ErrorSet) { |
| 2132 | return self.store(lhs, rhs, Type.anyerror, 0); | 2130 | return func.store(lhs, rhs, Type.anyerror, 0); |
| 2133 | } | 2131 | } |
| 2134 | | 2132 | |
| 2135 | const len = @intCast(u32, ty.abiSize(self.target)); | 2133 | const len = @intCast(u32, ty.abiSize(func.target)); |
| 2136 | return self.memcpy(lhs, rhs, .{ .imm32 = len }); | 2134 | return func.memcpy(lhs, rhs, .{ .imm32 = len }); |
| 2137 | }, | 2135 | }, |
| 2138 | .Struct, .Array, .Union, .Vector => { | 2136 | .Struct, .Array, .Union, .Vector => { |
| 2139 | const len = @intCast(u32, ty.abiSize(self.target)); | 2137 | const len = @intCast(u32, ty.abiSize(func.target)); |
| 2140 | return self.memcpy(lhs, rhs, .{ .imm32 = len }); | 2138 | return func.memcpy(lhs, rhs, .{ .imm32 = len }); |
| 2141 | }, | 2139 | }, |
| 2142 | .Pointer => { | 2140 | .Pointer => { |
| 2143 | if (ty.isSlice()) { | 2141 | if (ty.isSlice()) { |
| 2144 | // store pointer first | 2142 | // store pointer first |
| 2145 | // lower it to the stack so we do not have to store rhs into a local first | 2143 | // lower it to the stack so we do not have to store rhs into a local first |
| 2146 | try self.emitWValue(lhs); | 2144 | try func.emitWValue(lhs); |
| 2147 | const ptr_local = try self.load(rhs, Type.usize, 0); | 2145 | const ptr_local = try func.load(rhs, Type.usize, 0); |
| 2148 | try self.store(.{ .stack = {} }, ptr_local, Type.usize, 0 + lhs.offset()); | 2146 | try func.store(.{ .stack = {} }, ptr_local, Type.usize, 0 + lhs.offset()); |
| 2149 | | 2147 | |
| 2150 | // retrieve length from rhs, and store that alongside lhs as well | 2148 | // retrieve length from rhs, and store that alongside lhs as well |
| 2151 | try self.emitWValue(lhs); | 2149 | try func.emitWValue(lhs); |
| 2152 | const len_local = try self.load(rhs, Type.usize, self.ptrSize()); | 2150 | const len_local = try func.load(rhs, Type.usize, func.ptrSize()); |
| 2153 | try self.store(.{ .stack = {} }, len_local, Type.usize, self.ptrSize() + lhs.offset()); | 2151 | try func.store(.{ .stack = {} }, len_local, Type.usize, func.ptrSize() + lhs.offset()); |
| 2154 | return; | 2152 | return; |
| 2155 | } | 2153 | } |
| 2156 | }, | 2154 | }, |
| 2157 | .Int => if (ty.intInfo(self.target).bits > 64) { | 2155 | .Int => if (ty.intInfo(func.target).bits > 64) { |
| 2158 | try self.emitWValue(lhs); | 2156 | try func.emitWValue(lhs); |
| 2159 | const lsb = try self.load(rhs, Type.u64, 0); | 2157 | const lsb = try func.load(rhs, Type.u64, 0); |
| 2160 | try self.store(.{ .stack = {} }, lsb, Type.u64, 0 + lhs.offset()); | 2158 | try func.store(.{ .stack = {} }, lsb, Type.u64, 0 + lhs.offset()); |
| 2161 | | 2159 | |
| 2162 | try self.emitWValue(lhs); | 2160 | try func.emitWValue(lhs); |
| 2163 | const msb = try self.load(rhs, Type.u64, 8); | 2161 | const msb = try func.load(rhs, Type.u64, 8); |
| 2164 | try self.store(.{ .stack = {} }, msb, Type.u64, 8 + lhs.offset()); | 2162 | try func.store(.{ .stack = {} }, msb, Type.u64, 8 + lhs.offset()); |
| 2165 | return; | 2163 | return; |
| 2166 | }, | 2164 | }, |
| 2167 | else => {}, | 2165 | else => {}, |
| 2168 | } | 2166 | } |
| 2169 | try self.emitWValue(lhs); | 2167 | try func.emitWValue(lhs); |
| 2170 | // In this case we're actually interested in storing the stack position | 2168 | // In this case we're actually interested in storing the stack position |
| 2171 | // into lhs, so we calculate that and emit that instead | 2169 | // into lhs, so we calculate that and emit that instead |
| 2172 | try self.lowerToStack(rhs); | 2170 | try func.lowerToStack(rhs); |
| 2173 | | 2171 | |
| 2174 | const valtype = typeToValtype(ty, self.target); | 2172 | const valtype = typeToValtype(ty, func.target); |
| 2175 | const abi_size = @intCast(u8, ty.abiSize(self.target)); | 2173 | const abi_size = @intCast(u8, ty.abiSize(func.target)); |
| 2176 | | 2174 | |
| 2177 | const opcode = buildOpcode(.{ | 2175 | const opcode = buildOpcode(.{ |
| 2178 | .valtype1 = valtype, | 2176 | .valtype1 = valtype, |
| ... | @@ -2181,64 +2179,64 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro | ... | @@ -2181,64 +2179,64 @@ fn store(self: *Self, lhs: WValue, rhs: WValue, ty: Type, offset: u32) InnerErro |
| 2181 | }); | 2179 | }); |
| 2182 | | 2180 | |
| 2183 | // store rhs value at stack pointer's location in memory | 2181 | // store rhs value at stack pointer's location in memory |
| 2184 | try self.addMemArg( | 2182 | try func.addMemArg( |
| 2185 | Mir.Inst.Tag.fromOpcode(opcode), | 2183 | Mir.Inst.Tag.fromOpcode(opcode), |
| 2186 | .{ .offset = offset + lhs.offset(), .alignment = ty.abiAlignment(self.target) }, | 2184 | .{ .offset = offset + lhs.offset(), .alignment = ty.abiAlignment(func.target) }, |
| 2187 | ); | 2185 | ); |
| 2188 | } | 2186 | } |
| 2189 | | 2187 | |
| 2190 | fn airLoad(self: *Self, inst: Air.Inst.Index) InnerError!void { | 2188 | fn airLoad(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2191 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 2189 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 2192 | const operand = try self.resolveInst(ty_op.operand); | 2190 | const operand = try func.resolveInst(ty_op.operand); |
| 2193 | const ty = self.air.getRefType(ty_op.ty); | 2191 | const ty = func.air.getRefType(ty_op.ty); |
| 2194 | | 2192 | |
| 2195 | if (!ty.hasRuntimeBitsIgnoreComptime()) return self.finishAir(inst, .none, &.{ty_op.operand}); | 2193 | if (!ty.hasRuntimeBitsIgnoreComptime()) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 2196 | | 2194 | |
| 2197 | const result = result: { | 2195 | const result = result: { |
| 2198 | if (isByRef(ty, self.target)) { | 2196 | if (isByRef(ty, func.target)) { |
| 2199 | const new_local = try self.allocStack(ty); | 2197 | const new_local = try func.allocStack(ty); |
| 2200 | try self.store(new_local, operand, ty, 0); | 2198 | try func.store(new_local, operand, ty, 0); |
| 2201 | break :result new_local; | 2199 | break :result new_local; |
| 2202 | } | 2200 | } |
| 2203 | | 2201 | |
| 2204 | const stack_loaded = try self.load(operand, ty, 0); | 2202 | const stack_loaded = try func.load(operand, ty, 0); |
| 2205 | break :result try stack_loaded.toLocal(self, ty); | 2203 | break :result try stack_loaded.toLocal(func, ty); |
| 2206 | }; | 2204 | }; |
| 2207 | self.finishAir(inst, result, &.{ty_op.operand}); | 2205 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 2208 | } | 2206 | } |
| 2209 | | 2207 | |
| 2210 | /// Loads an operand from the linear memory section. | 2208 | /// Loads an operand from the linear memory section. |
| 2211 | /// NOTE: Leaves the value on the stack. | 2209 | /// NOTE: Leaves the value on the stack. |
| 2212 | fn load(self: *Self, operand: WValue, ty: Type, offset: u32) InnerError!WValue { | 2210 | fn load(func: *CodeGen, operand: WValue, ty: Type, offset: u32) InnerError!WValue { |
| 2213 | // load local's value from memory by its stack position | 2211 | // load local's value from memory by its stack position |
| 2214 | try self.emitWValue(operand); | 2212 | try func.emitWValue(operand); |
| 2215 | | 2213 | |
| 2216 | const abi_size = @intCast(u8, ty.abiSize(self.target)); | 2214 | const abi_size = @intCast(u8, ty.abiSize(func.target)); |
| 2217 | const opcode = buildOpcode(.{ | 2215 | const opcode = buildOpcode(.{ |
| 2218 | .valtype1 = typeToValtype(ty, self.target), | 2216 | .valtype1 = typeToValtype(ty, func.target), |
| 2219 | .width = abi_size * 8, | 2217 | .width = abi_size * 8, |
| 2220 | .op = .load, | 2218 | .op = .load, |
| 2221 | .signedness = .unsigned, | 2219 | .signedness = .unsigned, |
| 2222 | }); | 2220 | }); |
| 2223 | | 2221 | |
| 2224 | try self.addMemArg( | 2222 | try func.addMemArg( |
| 2225 | Mir.Inst.Tag.fromOpcode(opcode), | 2223 | Mir.Inst.Tag.fromOpcode(opcode), |
| 2226 | .{ .offset = offset + operand.offset(), .alignment = ty.abiAlignment(self.target) }, | 2224 | .{ .offset = offset + operand.offset(), .alignment = ty.abiAlignment(func.target) }, |
| 2227 | ); | 2225 | ); |
| 2228 | | 2226 | |
| 2229 | return WValue{ .stack = {} }; | 2227 | return WValue{ .stack = {} }; |
| 2230 | } | 2228 | } |
| 2231 | | 2229 | |
| 2232 | fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!void { | 2230 | fn airArg(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2233 | const arg_index = self.arg_index; | 2231 | const arg_index = func.arg_index; |
| 2234 | const arg = self.args[arg_index]; | 2232 | const arg = func.args[arg_index]; |
| 2235 | const cc = self.decl.ty.fnInfo().cc; | 2233 | const cc = func.decl.ty.fnInfo().cc; |
| 2236 | const arg_ty = self.air.typeOfIndex(inst); | 2234 | const arg_ty = func.air.typeOfIndex(inst); |
| 2237 | if (cc == .C) { | 2235 | if (cc == .C) { |
| 2238 | const arg_classes = abi.classifyType(arg_ty, self.target); | 2236 | const arg_classes = abi.classifyType(arg_ty, func.target); |
| 2239 | for (arg_classes) |class| { | 2237 | for (arg_classes) |class| { |
| 2240 | if (class != .none) { | 2238 | if (class != .none) { |
| 2241 | self.arg_index += 1; | 2239 | func.arg_index += 1; |
| 2242 | } | 2240 | } |
| 2243 | } | 2241 | } |
| 2244 | | 2242 | |
| ... | @@ -2246,24 +2244,24 @@ fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -2246,24 +2244,24 @@ fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2246 | // we combine them into a single stack value | 2244 | // we combine them into a single stack value |
| 2247 | if (arg_classes[0] == .direct and arg_classes[1] == .direct) { | 2245 | if (arg_classes[0] == .direct and arg_classes[1] == .direct) { |
| 2248 | if (arg_ty.zigTypeTag() != .Int) { | 2246 | if (arg_ty.zigTypeTag() != .Int) { |
| 2249 | return self.fail( | 2247 | return func.fail( |
| 2250 | "TODO: Implement C-ABI argument for type '{}'", | 2248 | "TODO: Implement C-ABI argument for type '{}'", |
| 2251 | .{arg_ty.fmt(self.bin_file.base.options.module.?)}, | 2249 | .{arg_ty.fmt(func.bin_file.base.options.module.?)}, |
| 2252 | ); | 2250 | ); |
| 2253 | } | 2251 | } |
| 2254 | const result = try self.allocStack(arg_ty); | 2252 | const result = try func.allocStack(arg_ty); |
| 2255 | try self.store(result, arg, Type.u64, 0); | 2253 | try func.store(result, arg, Type.u64, 0); |
| 2256 | try self.store(result, self.args[arg_index + 1], Type.u64, 8); | 2254 | try func.store(result, func.args[arg_index + 1], Type.u64, 8); |
| 2257 | return self.finishAir(inst, arg, &.{}); | 2255 | return func.finishAir(inst, arg, &.{}); |
| 2258 | } | 2256 | } |
| 2259 | } else { | 2257 | } else { |
| 2260 | self.arg_index += 1; | 2258 | func.arg_index += 1; |
| 2261 | } | 2259 | } |
| 2262 | | 2260 | |
| 2263 | switch (self.debug_output) { | 2261 | switch (func.debug_output) { |
| 2264 | .dwarf => |dwarf| { | 2262 | .dwarf => |dwarf| { |
| 2265 | // TODO: Get the original arg index rather than wasm arg index | 2263 | // TODO: Get the original arg index rather than wasm arg index |
| 2266 | const name = self.mod_fn.getParamName(self.bin_file.base.options.module.?, arg_index); | 2264 | const name = func.mod_fn.getParamName(func.bin_file.base.options.module.?, arg_index); |
| 2267 | const leb_size = link.File.Wasm.getULEB128Size(arg.local.value); | 2265 | const leb_size = link.File.Wasm.getULEB128Size(arg.local.value); |
| 2268 | const dbg_info = &dwarf.dbg_info; | 2266 | const dbg_info = &dwarf.dbg_info; |
| 2269 | try dbg_info.ensureUnusedCapacity(3 + leb_size + 5 + name.len + 1); | 2267 | try dbg_info.ensureUnusedCapacity(3 + leb_size + 5 + name.len + 1); |
| ... | @@ -2279,196 +2277,196 @@ fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -2279,196 +2277,196 @@ fn airArg(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 2279 | std.dwarf.OP.WASM_local, | 2277 | std.dwarf.OP.WASM_local, |
| 2280 | }); | 2278 | }); |
| 2281 | leb.writeULEB128(dbg_info.writer(), arg.local.value) catch unreachable; | 2279 | leb.writeULEB128(dbg_info.writer(), arg.local.value) catch unreachable; |
| 2282 | try self.addDbgInfoTypeReloc(arg_ty); | 2280 | try func.addDbgInfoTypeReloc(arg_ty); |
| 2283 | dbg_info.appendSliceAssumeCapacity(name); | 2281 | dbg_info.appendSliceAssumeCapacity(name); |
| 2284 | dbg_info.appendAssumeCapacity(0); | 2282 | dbg_info.appendAssumeCapacity(0); |
| 2285 | }, | 2283 | }, |
| 2286 | else => {}, | 2284 | else => {}, |
| 2287 | } | 2285 | } |
| 2288 | | 2286 | |
| 2289 | self.finishAir(inst, arg, &.{}); | 2287 | func.finishAir(inst, arg, &.{}); |
| 2290 | } | 2288 | } |
| 2291 | | 2289 | |
| 2292 | fn airBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { | 2290 | fn airBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 2293 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 2291 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 2294 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 2292 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 2295 | const lhs = try self.resolveInst(bin_op.lhs); | 2293 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2296 | const rhs = try self.resolveInst(bin_op.rhs); | 2294 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2297 | const ty = self.air.typeOf(bin_op.lhs); | 2295 | const ty = func.air.typeOf(bin_op.lhs); |
| 2298 | | 2296 | |
| 2299 | const stack_value = try self.binOp(lhs, rhs, ty, op); | 2297 | const stack_value = try func.binOp(lhs, rhs, ty, op); |
| 2300 | self.finishAir(inst, try stack_value.toLocal(self, ty), &.{ bin_op.lhs, bin_op.rhs }); | 2298 | func.finishAir(inst, try stack_value.toLocal(func, ty), &.{ bin_op.lhs, bin_op.rhs }); |
| 2301 | } | 2299 | } |
| 2302 | | 2300 | |
| 2303 | /// Performs a binary operation on the given `WValue`'s | 2301 | /// Performs a binary operation on the given `WValue`'s |
| 2304 | /// NOTE: THis leaves the value on top of the stack. | 2302 | /// NOTE: THis leaves the value on top of the stack. |
| 2305 | fn binOp(self: *Self, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WValue { | 2303 | fn binOp(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WValue { |
| 2306 | assert(!(lhs != .stack and rhs == .stack)); | 2304 | assert(!(lhs != .stack and rhs == .stack)); |
| 2307 | if (isByRef(ty, self.target)) { | 2305 | if (isByRef(ty, func.target)) { |
| 2308 | if (ty.zigTypeTag() == .Int) { | 2306 | if (ty.zigTypeTag() == .Int) { |
| 2309 | return self.binOpBigInt(lhs, rhs, ty, op); | 2307 | return func.binOpBigInt(lhs, rhs, ty, op); |
| 2310 | } else { | 2308 | } else { |
| 2311 | return self.fail( | 2309 | return func.fail( |
| 2312 | "TODO: Implement binary operation for type: {}", | 2310 | "TODO: Implement binary operation for type: {}", |
| 2313 | .{ty.fmt(self.bin_file.base.options.module.?)}, | 2311 | .{ty.fmt(func.bin_file.base.options.module.?)}, |
| 2314 | ); | 2312 | ); |
| 2315 | } | 2313 | } |
| 2316 | } | 2314 | } |
| 2317 | | 2315 | |
| 2318 | if (ty.isAnyFloat() and ty.floatBits(self.target) == 16) { | 2316 | if (ty.isAnyFloat() and ty.floatBits(func.target) == 16) { |
| 2319 | return self.binOpFloat16(lhs, rhs, op); | 2317 | return func.binOpFloat16(lhs, rhs, op); |
| 2320 | } | 2318 | } |
| 2321 | | 2319 | |
| 2322 | const opcode: wasm.Opcode = buildOpcode(.{ | 2320 | const opcode: wasm.Opcode = buildOpcode(.{ |
| 2323 | .op = op, | 2321 | .op = op, |
| 2324 | .valtype1 = typeToValtype(ty, self.target), | 2322 | .valtype1 = typeToValtype(ty, func.target), |
| 2325 | .signedness = if (ty.isSignedInt()) .signed else .unsigned, | 2323 | .signedness = if (ty.isSignedInt()) .signed else .unsigned, |
| 2326 | }); | 2324 | }); |
| 2327 | try self.emitWValue(lhs); | 2325 | try func.emitWValue(lhs); |
| 2328 | try self.emitWValue(rhs); | 2326 | try func.emitWValue(rhs); |
| 2329 | | 2327 | |
| 2330 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); | 2328 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 2331 | | 2329 | |
| 2332 | return WValue{ .stack = {} }; | 2330 | return WValue{ .stack = {} }; |
| 2333 | } | 2331 | } |
| 2334 | | 2332 | |
| 2335 | /// Performs a binary operation for 16-bit floats. | 2333 | /// Performs a binary operation for 16-bit floats. |
| 2336 | /// NOTE: Leaves the result value on the stack | 2334 | /// NOTE: Leaves the result value on the stack |
| 2337 | fn binOpFloat16(self: *Self, lhs: WValue, rhs: WValue, op: Op) InnerError!WValue { | 2335 | fn binOpFloat16(func: *CodeGen, lhs: WValue, rhs: WValue, op: Op) InnerError!WValue { |
| 2338 | const opcode: wasm.Opcode = buildOpcode(.{ .op = op, .valtype1 = .f32, .signedness = .unsigned }); | 2336 | const opcode: wasm.Opcode = buildOpcode(.{ .op = op, .valtype1 = .f32, .signedness = .unsigned }); |
| 2339 | _ = try self.fpext(lhs, Type.f16, Type.f32); | 2337 | _ = try func.fpext(lhs, Type.f16, Type.f32); |
| 2340 | _ = try self.fpext(rhs, Type.f16, Type.f32); | 2338 | _ = try func.fpext(rhs, Type.f16, Type.f32); |
| 2341 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); | 2339 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 2342 | | 2340 | |
| 2343 | return self.fptrunc(.{ .stack = {} }, Type.f32, Type.f16); | 2341 | return func.fptrunc(.{ .stack = {} }, Type.f32, Type.f16); |
| 2344 | } | 2342 | } |
| 2345 | | 2343 | |
| 2346 | fn binOpBigInt(self: *Self, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WValue { | 2344 | fn binOpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WValue { |
| 2347 | if (ty.intInfo(self.target).bits > 128) { | 2345 | if (ty.intInfo(func.target).bits > 128) { |
| 2348 | return self.fail("TODO: Implement binary operation for big integer", .{}); | 2346 | return func.fail("TODO: Implement binary operation for big integer", .{}); |
| 2349 | } | 2347 | } |
| 2350 | | 2348 | |
| 2351 | if (op != .add and op != .sub) { | 2349 | if (op != .add and op != .sub) { |
| 2352 | return self.fail("TODO: Implement binary operation for big integers", .{}); | 2350 | return func.fail("TODO: Implement binary operation for big integers", .{}); |
| 2353 | } | 2351 | } |
| 2354 | | 2352 | |
| 2355 | const result = try self.allocStack(ty); | 2353 | const result = try func.allocStack(ty); |
| 2356 | var lhs_high_bit = try (try self.load(lhs, Type.u64, 0)).toLocal(self, Type.u64); | 2354 | var lhs_high_bit = try (try func.load(lhs, Type.u64, 0)).toLocal(func, Type.u64); |
| 2357 | defer lhs_high_bit.free(self); | 2355 | defer lhs_high_bit.free(func); |
| 2358 | var rhs_high_bit = try (try self.load(rhs, Type.u64, 0)).toLocal(self, Type.u64); | 2356 | var rhs_high_bit = try (try func.load(rhs, Type.u64, 0)).toLocal(func, Type.u64); |
| 2359 | defer rhs_high_bit.free(self); | 2357 | defer rhs_high_bit.free(func); |
| 2360 | var high_op_res = try (try self.binOp(lhs_high_bit, rhs_high_bit, Type.u64, op)).toLocal(self, Type.u64); | 2358 | var high_op_res = try (try func.binOp(lhs_high_bit, rhs_high_bit, Type.u64, op)).toLocal(func, Type.u64); |
| 2361 | defer high_op_res.free(self); | 2359 | defer high_op_res.free(func); |
| 2362 | | 2360 | |
| 2363 | const lhs_low_bit = try self.load(lhs, Type.u64, 8); | 2361 | const lhs_low_bit = try func.load(lhs, Type.u64, 8); |
| 2364 | const rhs_low_bit = try self.load(rhs, Type.u64, 8); | 2362 | const rhs_low_bit = try func.load(rhs, Type.u64, 8); |
| 2365 | const low_op_res = try self.binOp(lhs_low_bit, rhs_low_bit, Type.u64, op); | 2363 | const low_op_res = try func.binOp(lhs_low_bit, rhs_low_bit, Type.u64, op); |
| 2366 | | 2364 | |
| 2367 | const lt = if (op == .add) blk: { | 2365 | const lt = if (op == .add) blk: { |
| 2368 | break :blk try self.cmp(high_op_res, rhs_high_bit, Type.u64, .lt); | 2366 | break :blk try func.cmp(high_op_res, rhs_high_bit, Type.u64, .lt); |
| 2369 | } else if (op == .sub) blk: { | 2367 | } else if (op == .sub) blk: { |
| 2370 | break :blk try self.cmp(lhs_high_bit, rhs_high_bit, Type.u64, .lt); | 2368 | break :blk try func.cmp(lhs_high_bit, rhs_high_bit, Type.u64, .lt); |
| 2371 | } else unreachable; | 2369 | } else unreachable; |
| 2372 | const tmp = try self.intcast(lt, Type.u32, Type.u64); | 2370 | const tmp = try func.intcast(lt, Type.u32, Type.u64); |
| 2373 | var tmp_op = try (try self.binOp(low_op_res, tmp, Type.u64, op)).toLocal(self, Type.u64); | 2371 | var tmp_op = try (try func.binOp(low_op_res, tmp, Type.u64, op)).toLocal(func, Type.u64); |
| 2374 | defer tmp_op.free(self); | 2372 | defer tmp_op.free(func); |
| 2375 | | 2373 | |
| 2376 | try self.store(result, high_op_res, Type.u64, 0); | 2374 | try func.store(result, high_op_res, Type.u64, 0); |
| 2377 | try self.store(result, tmp_op, Type.u64, 8); | 2375 | try func.store(result, tmp_op, Type.u64, 8); |
| 2378 | return result; | 2376 | return result; |
| 2379 | } | 2377 | } |
| 2380 | | 2378 | |
| 2381 | fn airWrapBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { | 2379 | fn airWrapBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 2382 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 2380 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 2383 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 2381 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 2384 | | 2382 | |
| 2385 | const lhs = try self.resolveInst(bin_op.lhs); | 2383 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2386 | const rhs = try self.resolveInst(bin_op.rhs); | 2384 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2387 | const ty = self.air.typeOf(bin_op.lhs); | 2385 | const ty = func.air.typeOf(bin_op.lhs); |
| 2388 | | 2386 | |
| 2389 | if (ty.zigTypeTag() == .Vector) { | 2387 | if (ty.zigTypeTag() == .Vector) { |
| 2390 | return self.fail("TODO: Implement wrapping arithmetic for vectors", .{}); | 2388 | return func.fail("TODO: Implement wrapping arithmetic for vectors", .{}); |
| 2391 | } | 2389 | } |
| 2392 | | 2390 | |
| 2393 | const result = try (try self.wrapBinOp(lhs, rhs, ty, op)).toLocal(self, ty); | 2391 | const result = try (try func.wrapBinOp(lhs, rhs, ty, op)).toLocal(func, ty); |
| 2394 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 2392 | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 2395 | } | 2393 | } |
| 2396 | | 2394 | |
| 2397 | /// Performs a wrapping binary operation. | 2395 | /// Performs a wrapping binary operation. |
| 2398 | /// Asserts rhs is not a stack value when lhs also isn't. | 2396 | /// Asserts rhs is not a stack value when lhs also isn't. |
| 2399 | /// NOTE: Leaves the result on the stack when its Type is <= 64 bits | 2397 | /// NOTE: Leaves the result on the stack when its Type is <= 64 bits |
| 2400 | fn wrapBinOp(self: *Self, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WValue { | 2398 | fn wrapBinOp(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: Op) InnerError!WValue { |
| 2401 | const bin_local = try self.binOp(lhs, rhs, ty, op); | 2399 | const bin_local = try func.binOp(lhs, rhs, ty, op); |
| 2402 | return self.wrapOperand(bin_local, ty); | 2400 | return func.wrapOperand(bin_local, ty); |
| 2403 | } | 2401 | } |
| 2404 | | 2402 | |
| 2405 | /// Wraps an operand based on a given type's bitsize. | 2403 | /// Wraps an operand based on a given type's bitsize. |
| 2406 | /// Asserts `Type` is <= 128 bits. | 2404 | /// Asserts `Type` is <= 128 bits. |
| 2407 | /// NOTE: When the Type is <= 64 bits, leaves the value on top of the stack. | 2405 | /// NOTE: When the Type is <= 64 bits, leaves the value on top of the stack. |
| 2408 | fn wrapOperand(self: *Self, operand: WValue, ty: Type) InnerError!WValue { | 2406 | fn wrapOperand(func: *CodeGen, operand: WValue, ty: Type) InnerError!WValue { |
| 2409 | assert(ty.abiSize(self.target) <= 16); | 2407 | assert(ty.abiSize(func.target) <= 16); |
| 2410 | const bitsize = ty.intInfo(self.target).bits; | 2408 | const bitsize = ty.intInfo(func.target).bits; |
| 2411 | const wasm_bits = toWasmBits(bitsize) orelse { | 2409 | const wasm_bits = toWasmBits(bitsize) orelse { |
| 2412 | return self.fail("TODO: Implement wrapOperand for bitsize '{d}'", .{bitsize}); | 2410 | return func.fail("TODO: Implement wrapOperand for bitsize '{d}'", .{bitsize}); |
| 2413 | }; | 2411 | }; |
| 2414 | | 2412 | |
| 2415 | if (wasm_bits == bitsize) return operand; | 2413 | if (wasm_bits == bitsize) return operand; |
| 2416 | | 2414 | |
| 2417 | if (wasm_bits == 128) { | 2415 | if (wasm_bits == 128) { |
| 2418 | assert(operand != .stack); | 2416 | assert(operand != .stack); |
| 2419 | const lsb = try self.load(operand, Type.u64, 8); | 2417 | const lsb = try func.load(operand, Type.u64, 8); |
| 2420 | | 2418 | |
| 2421 | const result_ptr = try self.allocStack(ty); | 2419 | const result_ptr = try func.allocStack(ty); |
| 2422 | try self.emitWValue(result_ptr); | 2420 | try func.emitWValue(result_ptr); |
| 2423 | try self.store(.{ .stack = {} }, lsb, Type.u64, 8 + result_ptr.offset()); | 2421 | try func.store(.{ .stack = {} }, lsb, Type.u64, 8 + result_ptr.offset()); |
| 2424 | const result = (@as(u64, 1) << @intCast(u6, 64 - (wasm_bits - bitsize))) - 1; | 2422 | const result = (@as(u64, 1) << @intCast(u6, 64 - (wasm_bits - bitsize))) - 1; |
| 2425 | try self.emitWValue(result_ptr); | 2423 | try func.emitWValue(result_ptr); |
| 2426 | _ = try self.load(operand, Type.u64, 0); | 2424 | _ = try func.load(operand, Type.u64, 0); |
| 2427 | try self.addImm64(result); | 2425 | try func.addImm64(result); |
| 2428 | try self.addTag(.i64_and); | 2426 | try func.addTag(.i64_and); |
| 2429 | try self.addMemArg(.i64_store, .{ .offset = result_ptr.offset(), .alignment = 8 }); | 2427 | try func.addMemArg(.i64_store, .{ .offset = result_ptr.offset(), .alignment = 8 }); |
| 2430 | return result_ptr; | 2428 | return result_ptr; |
| 2431 | } | 2429 | } |
| 2432 | | 2430 | |
| 2433 | const result = (@as(u64, 1) << @intCast(u6, bitsize)) - 1; | 2431 | const result = (@as(u64, 1) << @intCast(u6, bitsize)) - 1; |
| 2434 | try self.emitWValue(operand); | 2432 | try func.emitWValue(operand); |
| 2435 | if (bitsize <= 32) { | 2433 | if (bitsize <= 32) { |
| 2436 | try self.addImm32(@bitCast(i32, @intCast(u32, result))); | 2434 | try func.addImm32(@bitCast(i32, @intCast(u32, result))); |
| 2437 | try self.addTag(.i32_and); | 2435 | try func.addTag(.i32_and); |
| 2438 | } else if (bitsize <= 64) { | 2436 | } else if (bitsize <= 64) { |
| 2439 | try self.addImm64(result); | 2437 | try func.addImm64(result); |
| 2440 | try self.addTag(.i64_and); | 2438 | try func.addTag(.i64_and); |
| 2441 | } else unreachable; | 2439 | } else unreachable; |
| 2442 | | 2440 | |
| 2443 | return WValue{ .stack = {} }; | 2441 | return WValue{ .stack = {} }; |
| 2444 | } | 2442 | } |
| 2445 | | 2443 | |
| 2446 | fn lowerParentPtr(self: *Self, ptr_val: Value, ptr_child_ty: Type) InnerError!WValue { | 2444 | fn lowerParentPtr(func: *CodeGen, ptr_val: Value, ptr_child_ty: Type) InnerError!WValue { |
| 2447 | switch (ptr_val.tag()) { | 2445 | switch (ptr_val.tag()) { |
| 2448 | .decl_ref_mut => { | 2446 | .decl_ref_mut => { |
| 2449 | const decl_index = ptr_val.castTag(.decl_ref_mut).?.data.decl_index; | 2447 | const decl_index = ptr_val.castTag(.decl_ref_mut).?.data.decl_index; |
| 2450 | return self.lowerParentPtrDecl(ptr_val, decl_index); | 2448 | return func.lowerParentPtrDecl(ptr_val, decl_index); |
| 2451 | }, | 2449 | }, |
| 2452 | .decl_ref => { | 2450 | .decl_ref => { |
| 2453 | const decl_index = ptr_val.castTag(.decl_ref).?.data; | 2451 | const decl_index = ptr_val.castTag(.decl_ref).?.data; |
| 2454 | return self.lowerParentPtrDecl(ptr_val, decl_index); | 2452 | return func.lowerParentPtrDecl(ptr_val, decl_index); |
| 2455 | }, | 2453 | }, |
| 2456 | .variable => { | 2454 | .variable => { |
| 2457 | const decl_index = ptr_val.castTag(.variable).?.data.owner_decl; | 2455 | const decl_index = ptr_val.castTag(.variable).?.data.owner_decl; |
| 2458 | return self.lowerParentPtrDecl(ptr_val, decl_index); | 2456 | return func.lowerParentPtrDecl(ptr_val, decl_index); |
| 2459 | }, | 2457 | }, |
| 2460 | .field_ptr => { | 2458 | .field_ptr => { |
| 2461 | const field_ptr = ptr_val.castTag(.field_ptr).?.data; | 2459 | const field_ptr = ptr_val.castTag(.field_ptr).?.data; |
| 2462 | const parent_ty = field_ptr.container_ty; | 2460 | const parent_ty = field_ptr.container_ty; |
| 2463 | const parent_ptr = try self.lowerParentPtr(field_ptr.container_ptr, parent_ty); | 2461 | const parent_ptr = try func.lowerParentPtr(field_ptr.container_ptr, parent_ty); |
| 2464 | | 2462 | |
| 2465 | const offset = switch (parent_ty.zigTypeTag()) { | 2463 | const offset = switch (parent_ty.zigTypeTag()) { |
| 2466 | .Struct => blk: { | 2464 | .Struct => blk: { |
| 2467 | const offset = parent_ty.structFieldOffset(field_ptr.field_index, self.target); | 2465 | const offset = parent_ty.structFieldOffset(field_ptr.field_index, func.target); |
| 2468 | break :blk offset; | 2466 | break :blk offset; |
| 2469 | }, | 2467 | }, |
| 2470 | .Union => blk: { | 2468 | .Union => blk: { |
| 2471 | const layout: Module.Union.Layout = parent_ty.unionGetLayout(self.target); | 2469 | const layout: Module.Union.Layout = parent_ty.unionGetLayout(func.target); |
| 2472 | if (layout.payload_size == 0) break :blk 0; | 2470 | if (layout.payload_size == 0) break :blk 0; |
| 2473 | if (layout.payload_align > layout.tag_align) break :blk 0; | 2471 | if (layout.payload_align > layout.tag_align) break :blk 0; |
| 2474 | | 2472 | |
| ... | @@ -2479,7 +2477,7 @@ fn lowerParentPtr(self: *Self, ptr_val: Value, ptr_child_ty: Type) InnerError!WV | ... | @@ -2479,7 +2477,7 @@ fn lowerParentPtr(self: *Self, ptr_val: Value, ptr_child_ty: Type) InnerError!WV |
| 2479 | .Pointer => switch (parent_ty.ptrSize()) { | 2477 | .Pointer => switch (parent_ty.ptrSize()) { |
| 2480 | .Slice => switch (field_ptr.field_index) { | 2478 | .Slice => switch (field_ptr.field_index) { |
| 2481 | 0 => 0, | 2479 | 0 => 0, |
| 2482 | 1 => self.ptrSize(), | 2480 | 1 => func.ptrSize(), |
| 2483 | else => unreachable, | 2481 | else => unreachable, |
| 2484 | }, | 2482 | }, |
| 2485 | else => unreachable, | 2483 | else => unreachable, |
| ... | @@ -2506,8 +2504,8 @@ fn lowerParentPtr(self: *Self, ptr_val: Value, ptr_child_ty: Type) InnerError!WV | ... | @@ -2506,8 +2504,8 @@ fn lowerParentPtr(self: *Self, ptr_val: Value, ptr_child_ty: Type) InnerError!WV |
| 2506 | .elem_ptr => { | 2504 | .elem_ptr => { |
| 2507 | const elem_ptr = ptr_val.castTag(.elem_ptr).?.data; | 2505 | const elem_ptr = ptr_val.castTag(.elem_ptr).?.data; |
| 2508 | const index = elem_ptr.index; | 2506 | const index = elem_ptr.index; |
| 2509 | const offset = index * ptr_child_ty.abiSize(self.target); | 2507 | const offset = index * ptr_child_ty.abiSize(func.target); |
| 2510 | const array_ptr = try self.lowerParentPtr(elem_ptr.array_ptr, elem_ptr.elem_ty); | 2508 | const array_ptr = try func.lowerParentPtr(elem_ptr.array_ptr, elem_ptr.elem_ty); |
| 2511 | | 2509 | |
| 2512 | return WValue{ .memory_offset = .{ | 2510 | return WValue{ .memory_offset = .{ |
| 2513 | .pointer = array_ptr.memory, | 2511 | .pointer = array_ptr.memory, |
| ... | @@ -2516,27 +2514,27 @@ fn lowerParentPtr(self: *Self, ptr_val: Value, ptr_child_ty: Type) InnerError!WV | ... | @@ -2516,27 +2514,27 @@ fn lowerParentPtr(self: *Self, ptr_val: Value, ptr_child_ty: Type) InnerError!WV |
| 2516 | }, | 2514 | }, |
| 2517 | .opt_payload_ptr => { | 2515 | .opt_payload_ptr => { |
| 2518 | const payload_ptr = ptr_val.castTag(.opt_payload_ptr).?.data; | 2516 | const payload_ptr = ptr_val.castTag(.opt_payload_ptr).?.data; |
| 2519 | const parent_ptr = try self.lowerParentPtr(payload_ptr.container_ptr, payload_ptr.container_ty); | 2517 | const parent_ptr = try func.lowerParentPtr(payload_ptr.container_ptr, payload_ptr.container_ty); |
| 2520 | var buf: Type.Payload.ElemType = undefined; | 2518 | var buf: Type.Payload.ElemType = undefined; |
| 2521 | const payload_ty = payload_ptr.container_ty.optionalChild(&buf); | 2519 | const payload_ty = payload_ptr.container_ty.optionalChild(&buf); |
| 2522 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or payload_ty.optionalReprIsPayload()) { | 2520 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or payload_ty.optionalReprIsPayload()) { |
| 2523 | return parent_ptr; | 2521 | return parent_ptr; |
| 2524 | } | 2522 | } |
| 2525 | | 2523 | |
| 2526 | const abi_size = payload_ptr.container_ty.abiSize(self.target); | 2524 | const abi_size = payload_ptr.container_ty.abiSize(func.target); |
| 2527 | const offset = abi_size - payload_ty.abiSize(self.target); | 2525 | const offset = abi_size - payload_ty.abiSize(func.target); |
| 2528 | | 2526 | |
| 2529 | return WValue{ .memory_offset = .{ | 2527 | return WValue{ .memory_offset = .{ |
| 2530 | .pointer = parent_ptr.memory, | 2528 | .pointer = parent_ptr.memory, |
| 2531 | .offset = @intCast(u32, offset), | 2529 | .offset = @intCast(u32, offset), |
| 2532 | } }; | 2530 | } }; |
| 2533 | }, | 2531 | }, |
| 2534 | else => |tag| return self.fail("TODO: Implement lowerParentPtr for tag: {}", .{tag}), | 2532 | else => |tag| return func.fail("TODO: Implement lowerParentPtr for tag: {}", .{tag}), |
| 2535 | } | 2533 | } |
| 2536 | } | 2534 | } |
| 2537 | | 2535 | |
| 2538 | fn lowerParentPtrDecl(self: *Self, ptr_val: Value, decl_index: Module.Decl.Index) InnerError!WValue { | 2536 | fn lowerParentPtrDecl(func: *CodeGen, ptr_val: Value, decl_index: Module.Decl.Index) InnerError!WValue { |
| 2539 | const module = self.bin_file.base.options.module.?; | 2537 | const module = func.bin_file.base.options.module.?; |
| 2540 | const decl = module.declPtr(decl_index); | 2538 | const decl = module.declPtr(decl_index); |
| 2541 | module.markDeclAlive(decl); | 2539 | module.markDeclAlive(decl); |
| 2542 | var ptr_ty_payload: Type.Payload.ElemType = .{ | 2540 | var ptr_ty_payload: Type.Payload.ElemType = .{ |
| ... | @@ -2544,15 +2542,15 @@ fn lowerParentPtrDecl(self: *Self, ptr_val: Value, decl_index: Module.Decl.Index | ... | @@ -2544,15 +2542,15 @@ fn lowerParentPtrDecl(self: *Self, ptr_val: Value, decl_index: Module.Decl.Index |
| 2544 | .data = decl.ty, | 2542 | .data = decl.ty, |
| 2545 | }; | 2543 | }; |
| 2546 | const ptr_ty = Type.initPayload(&ptr_ty_payload.base); | 2544 | const ptr_ty = Type.initPayload(&ptr_ty_payload.base); |
| 2547 | return self.lowerDeclRefValue(.{ .ty = ptr_ty, .val = ptr_val }, decl_index); | 2545 | return func.lowerDeclRefValue(.{ .ty = ptr_ty, .val = ptr_val }, decl_index); |
| 2548 | } | 2546 | } |
| 2549 | | 2547 | |
| 2550 | fn lowerDeclRefValue(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) InnerError!WValue { | 2548 | fn lowerDeclRefValue(func: *CodeGen, tv: TypedValue, decl_index: Module.Decl.Index) InnerError!WValue { |
| 2551 | if (tv.ty.isSlice()) { | 2549 | if (tv.ty.isSlice()) { |
| 2552 | return WValue{ .memory = try self.bin_file.lowerUnnamedConst(tv, decl_index) }; | 2550 | return WValue{ .memory = try func.bin_file.lowerUnnamedConst(tv, decl_index) }; |
| 2553 | } | 2551 | } |
| 2554 | | 2552 | |
| 2555 | const module = self.bin_file.base.options.module.?; | 2553 | const module = func.bin_file.base.options.module.?; |
| 2556 | const decl = module.declPtr(decl_index); | 2554 | const decl = module.declPtr(decl_index); |
| 2557 | if (decl.ty.zigTypeTag() != .Fn and !decl.ty.hasRuntimeBitsIgnoreComptime()) { | 2555 | if (decl.ty.zigTypeTag() != .Fn and !decl.ty.hasRuntimeBitsIgnoreComptime()) { |
| 2558 | return WValue{ .imm32 = 0xaaaaaaaa }; | 2556 | return WValue{ .imm32 = 0xaaaaaaaa }; |
| ... | @@ -2562,7 +2560,7 @@ fn lowerDeclRefValue(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) | ... | @@ -2562,7 +2560,7 @@ fn lowerDeclRefValue(self: *Self, tv: TypedValue, decl_index: Module.Decl.Index) |
| 2562 | | 2560 | |
| 2563 | const target_sym_index = decl.link.wasm.sym_index; | 2561 | const target_sym_index = decl.link.wasm.sym_index; |
| 2564 | if (decl.ty.zigTypeTag() == .Fn) { | 2562 | if (decl.ty.zigTypeTag() == .Fn) { |
| 2565 | try self.bin_file.addTableFunction(target_sym_index); | 2563 | try func.bin_file.addTableFunction(target_sym_index); |
| 2566 | return WValue{ .function_index = target_sym_index }; | 2564 | return WValue{ .function_index = target_sym_index }; |
| 2567 | } else return WValue{ .memory = target_sym_index }; | 2565 | } else return WValue{ .memory = target_sym_index }; |
| 2568 | } | 2566 | } |
| ... | @@ -2583,21 +2581,21 @@ fn toTwosComplement(value: anytype, bits: u7) std.meta.Int(.unsigned, @typeInfo( | ... | @@ -2583,21 +2581,21 @@ fn toTwosComplement(value: anytype, bits: u7) std.meta.Int(.unsigned, @typeInfo( |
| 2583 | return @intCast(WantedT, result); | 2581 | return @intCast(WantedT, result); |
| 2584 | } | 2582 | } |
| 2585 | | 2583 | |
| 2586 | fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { | 2584 | fn lowerConstant(func: *CodeGen, val: Value, ty: Type) InnerError!WValue { |
| 2587 | if (val.isUndefDeep()) return self.emitUndefined(ty); | 2585 | if (val.isUndefDeep()) return func.emitUndefined(ty); |
| 2588 | if (val.castTag(.decl_ref)) |decl_ref| { | 2586 | if (val.castTag(.decl_ref)) |decl_ref| { |
| 2589 | const decl_index = decl_ref.data; | 2587 | const decl_index = decl_ref.data; |
| 2590 | return self.lowerDeclRefValue(.{ .ty = ty, .val = val }, decl_index); | 2588 | return func.lowerDeclRefValue(.{ .ty = ty, .val = val }, decl_index); |
| 2591 | } | 2589 | } |
| 2592 | if (val.castTag(.decl_ref_mut)) |decl_ref_mut| { | 2590 | if (val.castTag(.decl_ref_mut)) |decl_ref_mut| { |
| 2593 | const decl_index = decl_ref_mut.data.decl_index; | 2591 | const decl_index = decl_ref_mut.data.decl_index; |
| 2594 | return self.lowerDeclRefValue(.{ .ty = ty, .val = val }, decl_index); | 2592 | return func.lowerDeclRefValue(.{ .ty = ty, .val = val }, decl_index); |
| 2595 | } | 2593 | } |
| 2596 | const target = self.target; | 2594 | const target = func.target; |
| 2597 | switch (ty.zigTypeTag()) { | 2595 | switch (ty.zigTypeTag()) { |
| 2598 | .Void => return WValue{ .none = {} }, | 2596 | .Void => return WValue{ .none = {} }, |
| 2599 | .Int => { | 2597 | .Int => { |
| 2600 | const int_info = ty.intInfo(self.target); | 2598 | const int_info = ty.intInfo(func.target); |
| 2601 | switch (int_info.signedness) { | 2599 | switch (int_info.signedness) { |
| 2602 | .signed => switch (int_info.bits) { | 2600 | .signed => switch (int_info.bits) { |
| 2603 | 0...32 => return WValue{ .imm32 = @intCast(u32, toTwosComplement( | 2601 | 0...32 => return WValue{ .imm32 = @intCast(u32, toTwosComplement( |
| ... | @@ -2618,7 +2616,7 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { | ... | @@ -2618,7 +2616,7 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2618 | } | 2616 | } |
| 2619 | }, | 2617 | }, |
| 2620 | .Bool => return WValue{ .imm32 = @intCast(u32, val.toUnsignedInt(target)) }, | 2618 | .Bool => return WValue{ .imm32 = @intCast(u32, val.toUnsignedInt(target)) }, |
| 2621 | .Float => switch (ty.floatBits(self.target)) { | 2619 | .Float => switch (ty.floatBits(func.target)) { |
| 2622 | 16 => return WValue{ .imm32 = @bitCast(u16, val.toFloat(f16)) }, | 2620 | 16 => return WValue{ .imm32 = @bitCast(u16, val.toFloat(f16)) }, |
| 2623 | 32 => return WValue{ .float32 = val.toFloat(f32) }, | 2621 | 32 => return WValue{ .float32 = val.toFloat(f32) }, |
| 2624 | 64 => return WValue{ .float64 = val.toFloat(f64) }, | 2622 | 64 => return WValue{ .float64 = val.toFloat(f64) }, |
| ... | @@ -2626,11 +2624,11 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { | ... | @@ -2626,11 +2624,11 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2626 | }, | 2624 | }, |
| 2627 | .Pointer => switch (val.tag()) { | 2625 | .Pointer => switch (val.tag()) { |
| 2628 | .field_ptr, .elem_ptr, .opt_payload_ptr => { | 2626 | .field_ptr, .elem_ptr, .opt_payload_ptr => { |
| 2629 | return self.lowerParentPtr(val, ty.childType()); | 2627 | return func.lowerParentPtr(val, ty.childType()); |
| 2630 | }, | 2628 | }, |
| 2631 | .int_u64, .one => return WValue{ .imm32 = @intCast(u32, val.toUnsignedInt(target)) }, | 2629 | .int_u64, .one => return WValue{ .imm32 = @intCast(u32, val.toUnsignedInt(target)) }, |
| 2632 | .zero, .null_value => return WValue{ .imm32 = 0 }, | 2630 | .zero, .null_value => return WValue{ .imm32 = 0 }, |
| 2633 | else => return self.fail("Wasm TODO: lowerConstant for other const pointer tag {}", .{val.tag()}), | 2631 | else => return func.fail("Wasm TODO: lowerConstant for other const pointer tag {}", .{val.tag()}), |
| 2634 | }, | 2632 | }, |
| 2635 | .Enum => { | 2633 | .Enum => { |
| 2636 | if (val.castTag(.enum_field_index)) |field_index| { | 2634 | if (val.castTag(.enum_field_index)) |field_index| { |
| ... | @@ -2640,7 +2638,7 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { | ... | @@ -2640,7 +2638,7 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2640 | const enum_full = ty.cast(Type.Payload.EnumFull).?.data; | 2638 | const enum_full = ty.cast(Type.Payload.EnumFull).?.data; |
| 2641 | if (enum_full.values.count() != 0) { | 2639 | if (enum_full.values.count() != 0) { |
| 2642 | const tag_val = enum_full.values.keys()[field_index.data]; | 2640 | const tag_val = enum_full.values.keys()[field_index.data]; |
| 2643 | return self.lowerConstant(tag_val, enum_full.tag_ty); | 2641 | return func.lowerConstant(tag_val, enum_full.tag_ty); |
| 2644 | } else { | 2642 | } else { |
| 2645 | return WValue{ .imm32 = field_index.data }; | 2643 | return WValue{ .imm32 = field_index.data }; |
| 2646 | } | 2644 | } |
| ... | @@ -2649,19 +2647,19 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { | ... | @@ -2649,19 +2647,19 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2649 | const index = field_index.data; | 2647 | const index = field_index.data; |
| 2650 | const enum_data = ty.castTag(.enum_numbered).?.data; | 2648 | const enum_data = ty.castTag(.enum_numbered).?.data; |
| 2651 | const enum_val = enum_data.values.keys()[index]; | 2649 | const enum_val = enum_data.values.keys()[index]; |
| 2652 | return self.lowerConstant(enum_val, enum_data.tag_ty); | 2650 | return func.lowerConstant(enum_val, enum_data.tag_ty); |
| 2653 | }, | 2651 | }, |
| 2654 | else => return self.fail("TODO: lowerConstant for enum tag: {}", .{ty.tag()}), | 2652 | else => return func.fail("TODO: lowerConstant for enum tag: {}", .{ty.tag()}), |
| 2655 | } | 2653 | } |
| 2656 | } else { | 2654 | } else { |
| 2657 | var int_tag_buffer: Type.Payload.Bits = undefined; | 2655 | var int_tag_buffer: Type.Payload.Bits = undefined; |
| 2658 | const int_tag_ty = ty.intTagType(&int_tag_buffer); | 2656 | const int_tag_ty = ty.intTagType(&int_tag_buffer); |
| 2659 | return self.lowerConstant(val, int_tag_ty); | 2657 | return func.lowerConstant(val, int_tag_ty); |
| 2660 | } | 2658 | } |
| 2661 | }, | 2659 | }, |
| 2662 | .ErrorSet => switch (val.tag()) { | 2660 | .ErrorSet => switch (val.tag()) { |
| 2663 | .@"error" => { | 2661 | .@"error" => { |
| 2664 | const kv = try self.bin_file.base.options.module.?.getErrorValue(val.getError().?); | 2662 | const kv = try func.bin_file.base.options.module.?.getErrorValue(val.getError().?); |
| 2665 | return WValue{ .imm32 = kv.value }; | 2663 | return WValue{ .imm32 = kv.value }; |
| 2666 | }, | 2664 | }, |
| 2667 | else => return WValue{ .imm32 = 0 }, | 2665 | else => return WValue{ .imm32 = 0 }, |
| ... | @@ -2670,41 +2668,41 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { | ... | @@ -2670,41 +2668,41 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 2670 | const error_type = ty.errorUnionSet(); | 2668 | const error_type = ty.errorUnionSet(); |
| 2671 | const is_pl = val.errorUnionIsPayload(); | 2669 | const is_pl = val.errorUnionIsPayload(); |
| 2672 | const err_val = if (!is_pl) val else Value.initTag(.zero); | 2670 | const err_val = if (!is_pl) val else Value.initTag(.zero); |
| 2673 | return self.lowerConstant(err_val, error_type); | 2671 | return func.lowerConstant(err_val, error_type); |
| 2674 | }, | 2672 | }, |
| 2675 | .Optional => if (ty.optionalReprIsPayload()) { | 2673 | .Optional => if (ty.optionalReprIsPayload()) { |
| 2676 | var buf: Type.Payload.ElemType = undefined; | 2674 | var buf: Type.Payload.ElemType = undefined; |
| 2677 | const pl_ty = ty.optionalChild(&buf); | 2675 | const pl_ty = ty.optionalChild(&buf); |
| 2678 | if (val.castTag(.opt_payload)) |payload| { | 2676 | if (val.castTag(.opt_payload)) |payload| { |
| 2679 | return self.lowerConstant(payload.data, pl_ty); | 2677 | return func.lowerConstant(payload.data, pl_ty); |
| 2680 | } else if (val.isNull()) { | 2678 | } else if (val.isNull()) { |
| 2681 | return WValue{ .imm32 = 0 }; | 2679 | return WValue{ .imm32 = 0 }; |
| 2682 | } else { | 2680 | } else { |
| 2683 | return self.lowerConstant(val, pl_ty); | 2681 | return func.lowerConstant(val, pl_ty); |
| 2684 | } | 2682 | } |
| 2685 | } else { | 2683 | } else { |
| 2686 | const is_pl = val.tag() == .opt_payload; | 2684 | const is_pl = val.tag() == .opt_payload; |
| 2687 | return WValue{ .imm32 = if (is_pl) @as(u32, 1) else 0 }; | 2685 | return WValue{ .imm32 = if (is_pl) @as(u32, 1) else 0 }; |
| 2688 | }, | 2686 | }, |
| 2689 | else => |zig_type| return self.fail("Wasm TODO: LowerConstant for zigTypeTag {}", .{zig_type}), | 2687 | else => |zig_type| return func.fail("Wasm TODO: LowerConstant for zigTypeTag {}", .{zig_type}), |
| 2690 | } | 2688 | } |
| 2691 | } | 2689 | } |
| 2692 | | 2690 | |
| 2693 | fn emitUndefined(self: *Self, ty: Type) InnerError!WValue { | 2691 | fn emitUndefined(func: *CodeGen, ty: Type) InnerError!WValue { |
| 2694 | switch (ty.zigTypeTag()) { | 2692 | switch (ty.zigTypeTag()) { |
| 2695 | .Bool, .ErrorSet => return WValue{ .imm32 = 0xaaaaaaaa }, | 2693 | .Bool, .ErrorSet => return WValue{ .imm32 = 0xaaaaaaaa }, |
| 2696 | .Int => switch (ty.intInfo(self.target).bits) { | 2694 | .Int => switch (ty.intInfo(func.target).bits) { |
| 2697 | 0...32 => return WValue{ .imm32 = 0xaaaaaaaa }, | 2695 | 0...32 => return WValue{ .imm32 = 0xaaaaaaaa }, |
| 2698 | 33...64 => return WValue{ .imm64 = 0xaaaaaaaaaaaaaaaa }, | 2696 | 33...64 => return WValue{ .imm64 = 0xaaaaaaaaaaaaaaaa }, |
| 2699 | else => unreachable, | 2697 | else => unreachable, |
| 2700 | }, | 2698 | }, |
| 2701 | .Float => switch (ty.floatBits(self.target)) { | 2699 | .Float => switch (ty.floatBits(func.target)) { |
| 2702 | 16 => return WValue{ .imm32 = 0xaaaaaaaa }, | 2700 | 16 => return WValue{ .imm32 = 0xaaaaaaaa }, |
| 2703 | 32 => return WValue{ .float32 = @bitCast(f32, @as(u32, 0xaaaaaaaa)) }, | 2701 | 32 => return WValue{ .float32 = @bitCast(f32, @as(u32, 0xaaaaaaaa)) }, |
| 2704 | 64 => return WValue{ .float64 = @bitCast(f64, @as(u64, 0xaaaaaaaaaaaaaaaa)) }, | 2702 | 64 => return WValue{ .float64 = @bitCast(f64, @as(u64, 0xaaaaaaaaaaaaaaaa)) }, |
| 2705 | else => unreachable, | 2703 | else => unreachable, |
| 2706 | }, | 2704 | }, |
| 2707 | .Pointer => switch (self.arch()) { | 2705 | .Pointer => switch (func.arch()) { |
| 2708 | .wasm32 => return WValue{ .imm32 = 0xaaaaaaaa }, | 2706 | .wasm32 => return WValue{ .imm32 = 0xaaaaaaaa }, |
| 2709 | .wasm64 => return WValue{ .imm64 = 0xaaaaaaaaaaaaaaaa }, | 2707 | .wasm64 => return WValue{ .imm64 = 0xaaaaaaaaaaaaaaaa }, |
| 2710 | else => unreachable, | 2708 | else => unreachable, |
| ... | @@ -2713,22 +2711,22 @@ fn emitUndefined(self: *Self, ty: Type) InnerError!WValue { | ... | @@ -2713,22 +2711,22 @@ fn emitUndefined(self: *Self, ty: Type) InnerError!WValue { |
| 2713 | var buf: Type.Payload.ElemType = undefined; | 2711 | var buf: Type.Payload.ElemType = undefined; |
| 2714 | const pl_ty = ty.optionalChild(&buf); | 2712 | const pl_ty = ty.optionalChild(&buf); |
| 2715 | if (ty.optionalReprIsPayload()) { | 2713 | if (ty.optionalReprIsPayload()) { |
| 2716 | return self.emitUndefined(pl_ty); | 2714 | return func.emitUndefined(pl_ty); |
| 2717 | } | 2715 | } |
| 2718 | return WValue{ .imm32 = 0xaaaaaaaa }; | 2716 | return WValue{ .imm32 = 0xaaaaaaaa }; |
| 2719 | }, | 2717 | }, |
| 2720 | .ErrorUnion => { | 2718 | .ErrorUnion => { |
| 2721 | return WValue{ .imm32 = 0xaaaaaaaa }; | 2719 | return WValue{ .imm32 = 0xaaaaaaaa }; |
| 2722 | }, | 2720 | }, |
| 2723 | else => return self.fail("Wasm TODO: emitUndefined for type: {}\n", .{ty.zigTypeTag()}), | 2721 | else => return func.fail("Wasm TODO: emitUndefined for type: {}\n", .{ty.zigTypeTag()}), |
| 2724 | } | 2722 | } |
| 2725 | } | 2723 | } |
| 2726 | | 2724 | |
| 2727 | /// Returns a `Value` as a signed 32 bit value. | 2725 | /// Returns a `Value` as a signed 32 bit value. |
| 2728 | /// It's illegal to provide a value with a type that cannot be represented | 2726 | /// It's illegal to provide a value with a type that cannot be represented |
| 2729 | /// as an integer value. | 2727 | /// as an integer value. |
| 2730 | fn valueAsI32(self: Self, val: Value, ty: Type) i32 { | 2728 | fn valueAsI32(func: *const CodeGen, val: Value, ty: Type) i32 { |
| 2731 | const target = self.target; | 2729 | const target = func.target; |
| 2732 | switch (ty.zigTypeTag()) { | 2730 | switch (ty.zigTypeTag()) { |
| 2733 | .Enum => { | 2731 | .Enum => { |
| 2734 | if (val.castTag(.enum_field_index)) |field_index| { | 2732 | if (val.castTag(.enum_field_index)) |field_index| { |
| ... | @@ -2738,28 +2736,28 @@ fn valueAsI32(self: Self, val: Value, ty: Type) i32 { | ... | @@ -2738,28 +2736,28 @@ fn valueAsI32(self: Self, val: Value, ty: Type) i32 { |
| 2738 | const enum_full = ty.cast(Type.Payload.EnumFull).?.data; | 2736 | const enum_full = ty.cast(Type.Payload.EnumFull).?.data; |
| 2739 | if (enum_full.values.count() != 0) { | 2737 | if (enum_full.values.count() != 0) { |
| 2740 | const tag_val = enum_full.values.keys()[field_index.data]; | 2738 | const tag_val = enum_full.values.keys()[field_index.data]; |
| 2741 | return self.valueAsI32(tag_val, enum_full.tag_ty); | 2739 | return func.valueAsI32(tag_val, enum_full.tag_ty); |
| 2742 | } else return @bitCast(i32, field_index.data); | 2740 | } else return @bitCast(i32, field_index.data); |
| 2743 | }, | 2741 | }, |
| 2744 | .enum_numbered => { | 2742 | .enum_numbered => { |
| 2745 | const index = field_index.data; | 2743 | const index = field_index.data; |
| 2746 | const enum_data = ty.castTag(.enum_numbered).?.data; | 2744 | const enum_data = ty.castTag(.enum_numbered).?.data; |
| 2747 | return self.valueAsI32(enum_data.values.keys()[index], enum_data.tag_ty); | 2745 | return func.valueAsI32(enum_data.values.keys()[index], enum_data.tag_ty); |
| 2748 | }, | 2746 | }, |
| 2749 | else => unreachable, | 2747 | else => unreachable, |
| 2750 | } | 2748 | } |
| 2751 | } else { | 2749 | } else { |
| 2752 | var int_tag_buffer: Type.Payload.Bits = undefined; | 2750 | var int_tag_buffer: Type.Payload.Bits = undefined; |
| 2753 | const int_tag_ty = ty.intTagType(&int_tag_buffer); | 2751 | const int_tag_ty = ty.intTagType(&int_tag_buffer); |
| 2754 | return self.valueAsI32(val, int_tag_ty); | 2752 | return func.valueAsI32(val, int_tag_ty); |
| 2755 | } | 2753 | } |
| 2756 | }, | 2754 | }, |
| 2757 | .Int => switch (ty.intInfo(self.target).signedness) { | 2755 | .Int => switch (ty.intInfo(func.target).signedness) { |
| 2758 | .signed => return @truncate(i32, val.toSignedInt()), | 2756 | .signed => return @truncate(i32, val.toSignedInt()), |
| 2759 | .unsigned => return @bitCast(i32, @truncate(u32, val.toUnsignedInt(target))), | 2757 | .unsigned => return @bitCast(i32, @truncate(u32, val.toUnsignedInt(target))), |
| 2760 | }, | 2758 | }, |
| 2761 | .ErrorSet => { | 2759 | .ErrorSet => { |
| 2762 | const kv = self.bin_file.base.options.module.?.getErrorValue(val.getError().?) catch unreachable; // passed invalid `Value` to function | 2760 | const kv = func.bin_file.base.options.module.?.getErrorValue(val.getError().?) catch unreachable; // passed invalid `Value` to function |
| 2763 | return @bitCast(i32, kv.value); | 2761 | return @bitCast(i32, kv.value); |
| 2764 | }, | 2762 | }, |
| 2765 | .Bool => return @intCast(i32, val.toSignedInt()), | 2763 | .Bool => return @intCast(i32, val.toSignedInt()), |
| ... | @@ -2768,139 +2766,139 @@ fn valueAsI32(self: Self, val: Value, ty: Type) i32 { | ... | @@ -2768,139 +2766,139 @@ fn valueAsI32(self: Self, val: Value, ty: Type) i32 { |
| 2768 | } | 2766 | } |
| 2769 | } | 2767 | } |
| 2770 | | 2768 | |
| 2771 | fn airBlock(self: *Self, inst: Air.Inst.Index) InnerError!void { | 2769 | fn airBlock(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2772 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 2770 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 2773 | const block_ty = self.air.getRefType(ty_pl.ty); | 2771 | const block_ty = func.air.getRefType(ty_pl.ty); |
| 2774 | const wasm_block_ty = genBlockType(block_ty, self.target); | 2772 | const wasm_block_ty = genBlockType(block_ty, func.target); |
| 2775 | const extra = self.air.extraData(Air.Block, ty_pl.payload); | 2773 | const extra = func.air.extraData(Air.Block, ty_pl.payload); |
| 2776 | const body = self.air.extra[extra.end..][0..extra.data.body_len]; | 2774 | const body = func.air.extra[extra.end..][0..extra.data.body_len]; |
| 2777 | | 2775 | |
| 2778 | // if wasm_block_ty is non-empty, we create a register to store the temporary value | 2776 | // if wasm_block_ty is non-empty, we create a register to store the temporary value |
| 2779 | const block_result: WValue = if (wasm_block_ty != wasm.block_empty) blk: { | 2777 | const block_result: WValue = if (wasm_block_ty != wasm.block_empty) blk: { |
| 2780 | const ty: Type = if (isByRef(block_ty, self.target)) Type.u32 else block_ty; | 2778 | const ty: Type = if (isByRef(block_ty, func.target)) Type.u32 else block_ty; |
| 2781 | break :blk try self.ensureAllocLocal(ty); // make sure it's a clean local as it may never get overwritten | 2779 | break :blk try func.ensureAllocLocal(ty); // make sure it's a clean local as it may never get overwritten |
| 2782 | } else WValue.none; | 2780 | } else WValue.none; |
| 2783 | | 2781 | |
| 2784 | try self.startBlock(.block, wasm.block_empty); | 2782 | try func.startBlock(.block, wasm.block_empty); |
| 2785 | // Here we set the current block idx, so breaks know the depth to jump | 2783 | // Here we set the current block idx, so breaks know the depth to jump |
| 2786 | // to when breaking out. | 2784 | // to when breaking out. |
| 2787 | try self.blocks.putNoClobber(self.gpa, inst, .{ | 2785 | try func.blocks.putNoClobber(func.gpa, inst, .{ |
| 2788 | .label = self.block_depth, | 2786 | .label = func.block_depth, |
| 2789 | .value = block_result, | 2787 | .value = block_result, |
| 2790 | }); | 2788 | }); |
| 2791 | try self.genBody(body); | 2789 | try func.genBody(body); |
| 2792 | try self.endBlock(); | 2790 | try func.endBlock(); |
| 2793 | | 2791 | |
| 2794 | self.finishAir(inst, block_result, &.{}); | 2792 | func.finishAir(inst, block_result, &.{}); |
| 2795 | } | 2793 | } |
| 2796 | | 2794 | |
| 2797 | /// appends a new wasm block to the code section and increases the `block_depth` by 1 | 2795 | /// appends a new wasm block to the code section and increases the `block_depth` by 1 |
| 2798 | fn startBlock(self: *Self, block_tag: wasm.Opcode, valtype: u8) !void { | 2796 | fn startBlock(func: *CodeGen, block_tag: wasm.Opcode, valtype: u8) !void { |
| 2799 | self.block_depth += 1; | 2797 | func.block_depth += 1; |
| 2800 | try self.addInst(.{ | 2798 | try func.addInst(.{ |
| 2801 | .tag = Mir.Inst.Tag.fromOpcode(block_tag), | 2799 | .tag = Mir.Inst.Tag.fromOpcode(block_tag), |
| 2802 | .data = .{ .block_type = valtype }, | 2800 | .data = .{ .block_type = valtype }, |
| 2803 | }); | 2801 | }); |
| 2804 | } | 2802 | } |
| 2805 | | 2803 | |
| 2806 | /// Ends the current wasm block and decreases the `block_depth` by 1 | 2804 | /// Ends the current wasm block and decreases the `block_depth` by 1 |
| 2807 | fn endBlock(self: *Self) !void { | 2805 | fn endBlock(func: *CodeGen) !void { |
| 2808 | try self.addTag(.end); | 2806 | try func.addTag(.end); |
| 2809 | self.block_depth -= 1; | 2807 | func.block_depth -= 1; |
| 2810 | } | 2808 | } |
| 2811 | | 2809 | |
| 2812 | fn airLoop(self: *Self, inst: Air.Inst.Index) InnerError!void { | 2810 | fn airLoop(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2813 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 2811 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 2814 | const loop = self.air.extraData(Air.Block, ty_pl.payload); | 2812 | const loop = func.air.extraData(Air.Block, ty_pl.payload); |
| 2815 | const body = self.air.extra[loop.end..][0..loop.data.body_len]; | 2813 | const body = func.air.extra[loop.end..][0..loop.data.body_len]; |
| 2816 | | 2814 | |
| 2817 | // result type of loop is always 'noreturn', meaning we can always | 2815 | // result type of loop is always 'noreturn', meaning we can always |
| 2818 | // emit the wasm type 'block_empty'. | 2816 | // emit the wasm type 'block_empty'. |
| 2819 | try self.startBlock(.loop, wasm.block_empty); | 2817 | try func.startBlock(.loop, wasm.block_empty); |
| 2820 | try self.genBody(body); | 2818 | try func.genBody(body); |
| 2821 | | 2819 | |
| 2822 | // breaking to the index of a loop block will continue the loop instead | 2820 | // breaking to the index of a loop block will continue the loop instead |
| 2823 | try self.addLabel(.br, 0); | 2821 | try func.addLabel(.br, 0); |
| 2824 | try self.endBlock(); | 2822 | try func.endBlock(); |
| 2825 | | 2823 | |
| 2826 | self.finishAir(inst, .none, &.{}); | 2824 | func.finishAir(inst, .none, &.{}); |
| 2827 | } | 2825 | } |
| 2828 | | 2826 | |
| 2829 | fn airCondBr(self: *Self, inst: Air.Inst.Index) InnerError!void { | 2827 | fn airCondBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2830 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 2828 | const pl_op = func.air.instructions.items(.data)[inst].pl_op; |
| 2831 | const condition = try self.resolveInst(pl_op.operand); | 2829 | const condition = try func.resolveInst(pl_op.operand); |
| 2832 | const extra = self.air.extraData(Air.CondBr, pl_op.payload); | 2830 | const extra = func.air.extraData(Air.CondBr, pl_op.payload); |
| 2833 | const then_body = self.air.extra[extra.end..][0..extra.data.then_body_len]; | 2831 | const then_body = func.air.extra[extra.end..][0..extra.data.then_body_len]; |
| 2834 | const else_body = self.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; | 2832 | const else_body = func.air.extra[extra.end + then_body.len ..][0..extra.data.else_body_len]; |
| 2835 | const liveness_condbr = self.liveness.getCondBr(inst); | 2833 | const liveness_condbr = func.liveness.getCondBr(inst); |
| 2836 | | 2834 | |
| 2837 | // result type is always noreturn, so use `block_empty` as type. | 2835 | // result type is always noreturn, so use `block_empty` as type. |
| 2838 | try self.startBlock(.block, wasm.block_empty); | 2836 | try func.startBlock(.block, wasm.block_empty); |
| 2839 | // emit the conditional value | 2837 | // emit the conditional value |
| 2840 | try self.emitWValue(condition); | 2838 | try func.emitWValue(condition); |
| 2841 | | 2839 | |
| 2842 | // we inserted the block in front of the condition | 2840 | // we inserted the block in front of the condition |
| 2843 | // so now check if condition matches. If not, break outside this block | 2841 | // so now check if condition matches. If not, break outside this block |
| 2844 | // and continue with the then codepath | 2842 | // and continue with the then codepath |
| 2845 | try self.addLabel(.br_if, 0); | 2843 | try func.addLabel(.br_if, 0); |
| 2846 | | 2844 | |
| 2847 | try self.branches.ensureUnusedCapacity(self.gpa, 2); | 2845 | try func.branches.ensureUnusedCapacity(func.gpa, 2); |
| 2848 | | 2846 | |
| 2849 | self.branches.appendAssumeCapacity(.{}); | 2847 | func.branches.appendAssumeCapacity(.{}); |
| 2850 | try self.currentBranch().values.ensureUnusedCapacity(self.gpa, @intCast(u32, liveness_condbr.else_deaths.len)); | 2848 | try func.currentBranch().values.ensureUnusedCapacity(func.gpa, @intCast(u32, liveness_condbr.else_deaths.len)); |
| 2851 | for (liveness_condbr.else_deaths) |death| { | 2849 | for (liveness_condbr.else_deaths) |death| { |
| 2852 | self.processDeath(Air.indexToRef(death)); | 2850 | func.processDeath(Air.indexToRef(death)); |
| 2853 | } | 2851 | } |
| 2854 | try self.genBody(else_body); | 2852 | try func.genBody(else_body); |
| 2855 | try self.endBlock(); | 2853 | try func.endBlock(); |
| 2856 | var else_stack = self.branches.pop(); | 2854 | var else_stack = func.branches.pop(); |
| 2857 | defer else_stack.deinit(self.gpa); | 2855 | defer else_stack.deinit(func.gpa); |
| 2858 | | 2856 | |
| 2859 | // Outer block that matches the condition | 2857 | // Outer block that matches the condition |
| 2860 | self.branches.appendAssumeCapacity(.{}); | 2858 | func.branches.appendAssumeCapacity(.{}); |
| 2861 | try self.currentBranch().values.ensureUnusedCapacity(self.gpa, @intCast(u32, liveness_condbr.then_deaths.len)); | 2859 | try func.currentBranch().values.ensureUnusedCapacity(func.gpa, @intCast(u32, liveness_condbr.then_deaths.len)); |
| 2862 | for (liveness_condbr.then_deaths) |death| { | 2860 | for (liveness_condbr.then_deaths) |death| { |
| 2863 | self.processDeath(Air.indexToRef(death)); | 2861 | func.processDeath(Air.indexToRef(death)); |
| 2864 | } | 2862 | } |
| 2865 | try self.genBody(then_body); | 2863 | try func.genBody(then_body); |
| 2866 | var then_stack = self.branches.pop(); | 2864 | var then_stack = func.branches.pop(); |
| 2867 | defer then_stack.deinit(self.gpa); | 2865 | defer then_stack.deinit(func.gpa); |
| 2868 | | 2866 | |
| 2869 | try self.mergeBranch(&else_stack); | 2867 | try func.mergeBranch(&else_stack); |
| 2870 | try self.mergeBranch(&then_stack); | 2868 | try func.mergeBranch(&then_stack); |
| 2871 | | 2869 | |
| 2872 | self.finishAir(inst, .none, &.{}); | 2870 | func.finishAir(inst, .none, &.{}); |
| 2873 | } | 2871 | } |
| 2874 | | 2872 | |
| 2875 | fn mergeBranch(self: *Self, branch: *const Branch) !void { | 2873 | fn mergeBranch(func: *CodeGen, branch: *const Branch) !void { |
| 2876 | const parent = self.currentBranch(); | 2874 | const parent = func.currentBranch(); |
| 2877 | | 2875 | |
| 2878 | const target_slice = branch.values.entries.slice(); | 2876 | const target_slice = branch.values.entries.slice(); |
| 2879 | const target_keys = target_slice.items(.key); | 2877 | const target_keys = target_slice.items(.key); |
| 2880 | const target_values = target_slice.items(.value); | 2878 | const target_values = target_slice.items(.value); |
| 2881 | | 2879 | |
| 2882 | try parent.values.ensureUnusedCapacity(self.gpa, branch.values.count()); | 2880 | try parent.values.ensureUnusedCapacity(func.gpa, branch.values.count()); |
| 2883 | for (target_keys) |key, index| { | 2881 | for (target_keys) |key, index| { |
| 2884 | // TODO: process deaths from branches | 2882 | // TODO: process deaths from branches |
| 2885 | parent.values.putAssumeCapacity(key, target_values[index]); | 2883 | parent.values.putAssumeCapacity(key, target_values[index]); |
| 2886 | } | 2884 | } |
| 2887 | } | 2885 | } |
| 2888 | | 2886 | |
| 2889 | fn airCmp(self: *Self, inst: Air.Inst.Index, op: std.math.CompareOperator) InnerError!void { | 2887 | fn airCmp(func: *CodeGen, inst: Air.Inst.Index, op: std.math.CompareOperator) InnerError!void { |
| 2890 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 2888 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 2891 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 2889 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 2892 | | 2890 | |
| 2893 | const lhs = try self.resolveInst(bin_op.lhs); | 2891 | const lhs = try func.resolveInst(bin_op.lhs); |
| 2894 | const rhs = try self.resolveInst(bin_op.rhs); | 2892 | const rhs = try func.resolveInst(bin_op.rhs); |
| 2895 | const operand_ty = self.air.typeOf(bin_op.lhs); | 2893 | const operand_ty = func.air.typeOf(bin_op.lhs); |
| 2896 | const result = try (try self.cmp(lhs, rhs, operand_ty, op)).toLocal(self, Type.u32); // comparison result is always 32 bits | 2894 | const result = try (try func.cmp(lhs, rhs, operand_ty, op)).toLocal(func, Type.u32); // comparison result is always 32 bits |
| 2897 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 2895 | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 2898 | } | 2896 | } |
| 2899 | | 2897 | |
| 2900 | /// Compares two operands. | 2898 | /// Compares two operands. |
| 2901 | /// Asserts rhs is not a stack value when the lhs isn't a stack value either | 2899 | /// Asserts rhs is not a stack value when the lhs isn't a stack value either |
| 2902 | /// NOTE: This leaves the result on top of the stack, rather than a new local. | 2900 | /// NOTE: This leaves the result on top of the stack, rather than a new local. |
| 2903 | fn cmp(self: *Self, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareOperator) InnerError!WValue { | 2901 | fn cmp(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareOperator) InnerError!WValue { |
| 2904 | assert(!(lhs != .stack and rhs == .stack)); | 2902 | assert(!(lhs != .stack and rhs == .stack)); |
| 2905 | if (ty.zigTypeTag() == .Optional and !ty.optionalReprIsPayload()) { | 2903 | if (ty.zigTypeTag() == .Optional and !ty.optionalReprIsPayload()) { |
| 2906 | var buf: Type.Payload.ElemType = undefined; | 2904 | var buf: Type.Payload.ElemType = undefined; |
| ... | @@ -2909,28 +2907,28 @@ fn cmp(self: *Self, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareOper | ... | @@ -2909,28 +2907,28 @@ fn cmp(self: *Self, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareOper |
| 2909 | // When we hit this case, we must check the value of optionals | 2907 | // When we hit this case, we must check the value of optionals |
| 2910 | // that are not pointers. This means first checking against non-null for | 2908 | // that are not pointers. This means first checking against non-null for |
| 2911 | // both lhs and rhs, as well as checking the payload are matching of lhs and rhs | 2909 | // both lhs and rhs, as well as checking the payload are matching of lhs and rhs |
| 2912 | return self.cmpOptionals(lhs, rhs, ty, op); | 2910 | return func.cmpOptionals(lhs, rhs, ty, op); |
| 2913 | } | 2911 | } |
| 2914 | } else if (isByRef(ty, self.target)) { | 2912 | } else if (isByRef(ty, func.target)) { |
| 2915 | return self.cmpBigInt(lhs, rhs, ty, op); | 2913 | return func.cmpBigInt(lhs, rhs, ty, op); |
| 2916 | } else if (ty.isAnyFloat() and ty.floatBits(self.target) == 16) { | 2914 | } else if (ty.isAnyFloat() and ty.floatBits(func.target) == 16) { |
| 2917 | return self.cmpFloat16(lhs, rhs, op); | 2915 | return func.cmpFloat16(lhs, rhs, op); |
| 2918 | } | 2916 | } |
| 2919 | | 2917 | |
| 2920 | // ensure that when we compare pointers, we emit | 2918 | // ensure that when we compare pointers, we emit |
| 2921 | // the true pointer of a stack value, rather than the stack pointer. | 2919 | // the true pointer of a stack value, rather than the stack pointer. |
| 2922 | try self.lowerToStack(lhs); | 2920 | try func.lowerToStack(lhs); |
| 2923 | try self.lowerToStack(rhs); | 2921 | try func.lowerToStack(rhs); |
| 2924 | | 2922 | |
| 2925 | const signedness: std.builtin.Signedness = blk: { | 2923 | const signedness: std.builtin.Signedness = blk: { |
| 2926 | // by default we tell the operand type is unsigned (i.e. bools and enum values) | 2924 | // by default we tell the operand type is unsigned (i.e. bools and enum values) |
| 2927 | if (ty.zigTypeTag() != .Int) break :blk .unsigned; | 2925 | if (ty.zigTypeTag() != .Int) break :blk .unsigned; |
| 2928 | | 2926 | |
| 2929 | // incase of an actual integer, we emit the correct signedness | 2927 | // incase of an actual integer, we emit the correct signedness |
| 2930 | break :blk ty.intInfo(self.target).signedness; | 2928 | break :blk ty.intInfo(func.target).signedness; |
| 2931 | }; | 2929 | }; |
| 2932 | const opcode: wasm.Opcode = buildOpcode(.{ | 2930 | const opcode: wasm.Opcode = buildOpcode(.{ |
| 2933 | .valtype1 = typeToValtype(ty, self.target), | 2931 | .valtype1 = typeToValtype(ty, func.target), |
| 2934 | .op = switch (op) { | 2932 | .op = switch (op) { |
| 2935 | .lt => .lt, | 2933 | .lt => .lt, |
| 2936 | .lte => .le, | 2934 | .lte => .le, |
| ... | @@ -2941,14 +2939,14 @@ fn cmp(self: *Self, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareOper | ... | @@ -2941,14 +2939,14 @@ fn cmp(self: *Self, lhs: WValue, rhs: WValue, ty: Type, op: std.math.CompareOper |
| 2941 | }, | 2939 | }, |
| 2942 | .signedness = signedness, | 2940 | .signedness = signedness, |
| 2943 | }); | 2941 | }); |
| 2944 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); | 2942 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 2945 | | 2943 | |
| 2946 | return WValue{ .stack = {} }; | 2944 | return WValue{ .stack = {} }; |
| 2947 | } | 2945 | } |
| 2948 | | 2946 | |
| 2949 | /// Compares 16-bit floats | 2947 | /// Compares 16-bit floats |
| 2950 | /// NOTE: The result value remains on top of the stack. | 2948 | /// NOTE: The result value remains on top of the stack. |
| 2951 | fn cmpFloat16(self: *Self, lhs: WValue, rhs: WValue, op: std.math.CompareOperator) InnerError!WValue { | 2949 | fn cmpFloat16(func: *CodeGen, lhs: WValue, rhs: WValue, op: std.math.CompareOperator) InnerError!WValue { |
| 2952 | const opcode: wasm.Opcode = buildOpcode(.{ | 2950 | const opcode: wasm.Opcode = buildOpcode(.{ |
| 2953 | .op = switch (op) { | 2951 | .op = switch (op) { |
| 2954 | .lt => .lt, | 2952 | .lt => .lt, |
| ... | @@ -2961,200 +2959,200 @@ fn cmpFloat16(self: *Self, lhs: WValue, rhs: WValue, op: std.math.CompareOperato | ... | @@ -2961,200 +2959,200 @@ fn cmpFloat16(self: *Self, lhs: WValue, rhs: WValue, op: std.math.CompareOperato |
| 2961 | .valtype1 = .f32, | 2959 | .valtype1 = .f32, |
| 2962 | .signedness = .unsigned, | 2960 | .signedness = .unsigned, |
| 2963 | }); | 2961 | }); |
| 2964 | _ = try self.fpext(lhs, Type.f16, Type.f32); | 2962 | _ = try func.fpext(lhs, Type.f16, Type.f32); |
| 2965 | _ = try self.fpext(rhs, Type.f16, Type.f32); | 2963 | _ = try func.fpext(rhs, Type.f16, Type.f32); |
| 2966 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); | 2964 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 2967 | | 2965 | |
| 2968 | return WValue{ .stack = {} }; | 2966 | return WValue{ .stack = {} }; |
| 2969 | } | 2967 | } |
| 2970 | | 2968 | |
| 2971 | fn airCmpVector(self: *Self, inst: Air.Inst.Index) InnerError!void { | 2969 | fn airCmpVector(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2972 | _ = inst; | 2970 | _ = inst; |
| 2973 | return self.fail("TODO implement airCmpVector for wasm", .{}); | 2971 | return func.fail("TODO implement airCmpVector for wasm", .{}); |
| 2974 | } | 2972 | } |
| 2975 | | 2973 | |
| 2976 | fn airCmpLtErrorsLen(self: *Self, inst: Air.Inst.Index) InnerError!void { | 2974 | fn airCmpLtErrorsLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2977 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 2975 | const un_op = func.air.instructions.items(.data)[inst].un_op; |
| 2978 | const operand = try self.resolveInst(un_op); | 2976 | const operand = try func.resolveInst(un_op); |
| 2979 | | 2977 | |
| 2980 | _ = operand; | 2978 | _ = operand; |
| 2981 | return self.fail("TODO implement airCmpLtErrorsLen for wasm", .{}); | 2979 | return func.fail("TODO implement airCmpLtErrorsLen for wasm", .{}); |
| 2982 | } | 2980 | } |
| 2983 | | 2981 | |
| 2984 | fn airBr(self: *Self, inst: Air.Inst.Index) InnerError!void { | 2982 | fn airBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 2985 | const br = self.air.instructions.items(.data)[inst].br; | 2983 | const br = func.air.instructions.items(.data)[inst].br; |
| 2986 | const block = self.blocks.get(br.block_inst).?; | 2984 | const block = func.blocks.get(br.block_inst).?; |
| 2987 | | 2985 | |
| 2988 | // if operand has codegen bits we should break with a value | 2986 | // if operand has codegen bits we should break with a value |
| 2989 | if (self.air.typeOf(br.operand).hasRuntimeBitsIgnoreComptime()) { | 2987 | if (func.air.typeOf(br.operand).hasRuntimeBitsIgnoreComptime()) { |
| 2990 | const operand = try self.resolveInst(br.operand); | 2988 | const operand = try func.resolveInst(br.operand); |
| 2991 | try self.lowerToStack(operand); | 2989 | try func.lowerToStack(operand); |
| 2992 | | 2990 | |
| 2993 | if (block.value != .none) { | 2991 | if (block.value != .none) { |
| 2994 | try self.addLabel(.local_set, block.value.local.value); | 2992 | try func.addLabel(.local_set, block.value.local.value); |
| 2995 | } | 2993 | } |
| 2996 | } | 2994 | } |
| 2997 | | 2995 | |
| 2998 | // We map every block to its block index. | 2996 | // We map every block to its block index. |
| 2999 | // We then determine how far we have to jump to it by subtracting it from current block depth | 2997 | // We then determine how far we have to jump to it by subtracting it from current block depth |
| 3000 | const idx: u32 = self.block_depth - block.label; | 2998 | const idx: u32 = func.block_depth - block.label; |
| 3001 | try self.addLabel(.br, idx); | 2999 | try func.addLabel(.br, idx); |
| 3002 | | 3000 | |
| 3003 | self.finishAir(inst, .none, &.{br.operand}); | 3001 | func.finishAir(inst, .none, &.{br.operand}); |
| 3004 | } | 3002 | } |
| 3005 | | 3003 | |
| 3006 | fn airNot(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3004 | fn airNot(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3007 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3005 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3008 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3006 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3009 | | 3007 | |
| 3010 | const operand = try self.resolveInst(ty_op.operand); | 3008 | const operand = try func.resolveInst(ty_op.operand); |
| 3011 | const operand_ty = self.air.typeOf(ty_op.operand); | 3009 | const operand_ty = func.air.typeOf(ty_op.operand); |
| 3012 | | 3010 | |
| 3013 | const result = result: { | 3011 | const result = result: { |
| 3014 | if (operand_ty.zigTypeTag() == .Bool) { | 3012 | if (operand_ty.zigTypeTag() == .Bool) { |
| 3015 | try self.emitWValue(operand); | 3013 | try func.emitWValue(operand); |
| 3016 | try self.addTag(.i32_eqz); | 3014 | try func.addTag(.i32_eqz); |
| 3017 | const not_tmp = try self.allocLocal(operand_ty); | 3015 | const not_tmp = try func.allocLocal(operand_ty); |
| 3018 | try self.addLabel(.local_set, not_tmp.local.value); | 3016 | try func.addLabel(.local_set, not_tmp.local.value); |
| 3019 | break :result not_tmp; | 3017 | break :result not_tmp; |
| 3020 | } else { | 3018 | } else { |
| 3021 | const operand_bits = operand_ty.intInfo(self.target).bits; | 3019 | const operand_bits = operand_ty.intInfo(func.target).bits; |
| 3022 | const wasm_bits = toWasmBits(operand_bits) orelse { | 3020 | const wasm_bits = toWasmBits(operand_bits) orelse { |
| 3023 | return self.fail("TODO: Implement binary NOT for integer with bitsize '{d}'", .{operand_bits}); | 3021 | return func.fail("TODO: Implement binary NOT for integer with bitsize '{d}'", .{operand_bits}); |
| 3024 | }; | 3022 | }; |
| 3025 | | 3023 | |
| 3026 | switch (wasm_bits) { | 3024 | switch (wasm_bits) { |
| 3027 | 32 => { | 3025 | 32 => { |
| 3028 | const bin_op = try self.binOp(operand, .{ .imm32 = ~@as(u32, 0) }, operand_ty, .xor); | 3026 | const bin_op = try func.binOp(operand, .{ .imm32 = ~@as(u32, 0) }, operand_ty, .xor); |
| 3029 | break :result try (try self.wrapOperand(bin_op, operand_ty)).toLocal(self, operand_ty); | 3027 | break :result try (try func.wrapOperand(bin_op, operand_ty)).toLocal(func, operand_ty); |
| 3030 | }, | 3028 | }, |
| 3031 | 64 => { | 3029 | 64 => { |
| 3032 | const bin_op = try self.binOp(operand, .{ .imm64 = ~@as(u64, 0) }, operand_ty, .xor); | 3030 | const bin_op = try func.binOp(operand, .{ .imm64 = ~@as(u64, 0) }, operand_ty, .xor); |
| 3033 | break :result try (try self.wrapOperand(bin_op, operand_ty)).toLocal(self, operand_ty); | 3031 | break :result try (try func.wrapOperand(bin_op, operand_ty)).toLocal(func, operand_ty); |
| 3034 | }, | 3032 | }, |
| 3035 | 128 => { | 3033 | 128 => { |
| 3036 | const result_ptr = try self.allocStack(operand_ty); | 3034 | const result_ptr = try func.allocStack(operand_ty); |
| 3037 | try self.emitWValue(result_ptr); | 3035 | try func.emitWValue(result_ptr); |
| 3038 | const msb = try self.load(operand, Type.u64, 0); | 3036 | const msb = try func.load(operand, Type.u64, 0); |
| 3039 | const msb_xor = try self.binOp(msb, .{ .imm64 = ~@as(u64, 0) }, Type.u64, .xor); | 3037 | const msb_xor = try func.binOp(msb, .{ .imm64 = ~@as(u64, 0) }, Type.u64, .xor); |
| 3040 | try self.store(.{ .stack = {} }, msb_xor, Type.u64, 0 + result_ptr.offset()); | 3038 | try func.store(.{ .stack = {} }, msb_xor, Type.u64, 0 + result_ptr.offset()); |
| 3041 | | 3039 | |
| 3042 | try self.emitWValue(result_ptr); | 3040 | try func.emitWValue(result_ptr); |
| 3043 | const lsb = try self.load(operand, Type.u64, 8); | 3041 | const lsb = try func.load(operand, Type.u64, 8); |
| 3044 | const lsb_xor = try self.binOp(lsb, .{ .imm64 = ~@as(u64, 0) }, Type.u64, .xor); | 3042 | const lsb_xor = try func.binOp(lsb, .{ .imm64 = ~@as(u64, 0) }, Type.u64, .xor); |
| 3045 | try self.store(result_ptr, lsb_xor, Type.u64, 8 + result_ptr.offset()); | 3043 | try func.store(result_ptr, lsb_xor, Type.u64, 8 + result_ptr.offset()); |
| 3046 | break :result result_ptr; | 3044 | break :result result_ptr; |
| 3047 | }, | 3045 | }, |
| 3048 | else => unreachable, | 3046 | else => unreachable, |
| 3049 | } | 3047 | } |
| 3050 | } | 3048 | } |
| 3051 | }; | 3049 | }; |
| 3052 | self.finishAir(inst, result, &.{ty_op.operand}); | 3050 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3053 | } | 3051 | } |
| 3054 | | 3052 | |
| 3055 | fn airBreakpoint(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3053 | fn airBreakpoint(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3056 | // unsupported by wasm itself. Can be implemented once we support DWARF | 3054 | // unsupported by wasm itfunc. Can be implemented once we support DWARF |
| 3057 | // for wasm | 3055 | // for wasm |
| 3058 | self.finishAir(inst, .none, &.{}); | 3056 | func.finishAir(inst, .none, &.{}); |
| 3059 | } | 3057 | } |
| 3060 | | 3058 | |
| 3061 | fn airUnreachable(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3059 | fn airUnreachable(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3062 | try self.addTag(.@"unreachable"); | 3060 | try func.addTag(.@"unreachable"); |
| 3063 | self.finishAir(inst, .none, &.{}); | 3061 | func.finishAir(inst, .none, &.{}); |
| 3064 | } | 3062 | } |
| 3065 | | 3063 | |
| 3066 | fn airBitcast(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3064 | fn airBitcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3067 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3065 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3068 | const result = if (!self.liveness.isUnused(inst)) result: { | 3066 | const result = if (!func.liveness.isUnused(inst)) result: { |
| 3069 | const operand = try self.resolveInst(ty_op.operand); | 3067 | const operand = try func.resolveInst(ty_op.operand); |
| 3070 | break :result self.reuseOperand(ty_op.operand, operand); | 3068 | break :result func.reuseOperand(ty_op.operand, operand); |
| 3071 | } else WValue{ .none = {} }; | 3069 | } else WValue{ .none = {} }; |
| 3072 | self.finishAir(inst, result, &.{}); | 3070 | func.finishAir(inst, result, &.{}); |
| 3073 | } | 3071 | } |
| 3074 | | 3072 | |
| 3075 | fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3073 | fn airStructFieldPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3076 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 3074 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 3077 | const extra = self.air.extraData(Air.StructField, ty_pl.payload); | 3075 | const extra = func.air.extraData(Air.StructField, ty_pl.payload); |
| 3078 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{extra.data.struct_operand}); | 3076 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{extra.data.struct_operand}); |
| 3079 | | 3077 | |
| 3080 | const struct_ptr = try self.resolveInst(extra.data.struct_operand); | 3078 | const struct_ptr = try func.resolveInst(extra.data.struct_operand); |
| 3081 | const struct_ty = self.air.typeOf(extra.data.struct_operand).childType(); | 3079 | const struct_ty = func.air.typeOf(extra.data.struct_operand).childType(); |
| 3082 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(extra.data.field_index, self.target)) orelse { | 3080 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(extra.data.field_index, func.target)) orelse { |
| 3083 | const module = self.bin_file.base.options.module.?; | 3081 | const module = func.bin_file.base.options.module.?; |
| 3084 | return self.fail("Field type '{}' too big to fit into stack frame", .{ | 3082 | return func.fail("Field type '{}' too big to fit into stack frame", .{ |
| 3085 | struct_ty.structFieldType(extra.data.field_index).fmt(module), | 3083 | struct_ty.structFieldType(extra.data.field_index).fmt(module), |
| 3086 | }); | 3084 | }); |
| 3087 | }; | 3085 | }; |
| 3088 | const result = try self.structFieldPtr(struct_ptr, offset); | 3086 | const result = try func.structFieldPtr(struct_ptr, offset); |
| 3089 | self.finishAir(inst, result, &.{extra.data.struct_operand}); | 3087 | func.finishAir(inst, result, &.{extra.data.struct_operand}); |
| 3090 | } | 3088 | } |
| 3091 | | 3089 | |
| 3092 | fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u32) InnerError!void { | 3090 | fn airStructFieldPtrIndex(func: *CodeGen, inst: Air.Inst.Index, index: u32) InnerError!void { |
| 3093 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3091 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3094 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3092 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3095 | const struct_ptr = try self.resolveInst(ty_op.operand); | 3093 | const struct_ptr = try func.resolveInst(ty_op.operand); |
| 3096 | const struct_ty = self.air.typeOf(ty_op.operand).childType(); | 3094 | const struct_ty = func.air.typeOf(ty_op.operand).childType(); |
| 3097 | const field_ty = struct_ty.structFieldType(index); | 3095 | const field_ty = struct_ty.structFieldType(index); |
| 3098 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(index, self.target)) orelse { | 3096 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(index, func.target)) orelse { |
| 3099 | const module = self.bin_file.base.options.module.?; | 3097 | const module = func.bin_file.base.options.module.?; |
| 3100 | return self.fail("Field type '{}' too big to fit into stack frame", .{ | 3098 | return func.fail("Field type '{}' too big to fit into stack frame", .{ |
| 3101 | field_ty.fmt(module), | 3099 | field_ty.fmt(module), |
| 3102 | }); | 3100 | }); |
| 3103 | }; | 3101 | }; |
| 3104 | const result = try self.structFieldPtr(struct_ptr, offset); | 3102 | const result = try func.structFieldPtr(struct_ptr, offset); |
| 3105 | self.finishAir(inst, result, &.{ty_op.operand}); | 3103 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3106 | } | 3104 | } |
| 3107 | | 3105 | |
| 3108 | fn structFieldPtr(self: *Self, struct_ptr: WValue, offset: u32) InnerError!WValue { | 3106 | fn structFieldPtr(func: *CodeGen, struct_ptr: WValue, offset: u32) InnerError!WValue { |
| 3109 | switch (struct_ptr) { | 3107 | switch (struct_ptr) { |
| 3110 | .stack_offset => |stack_offset| { | 3108 | .stack_offset => |stack_offset| { |
| 3111 | return WValue{ .stack_offset = .{ .value = stack_offset.value + offset, .references = 1 } }; | 3109 | return WValue{ .stack_offset = .{ .value = stack_offset.value + offset, .references = 1 } }; |
| 3112 | }, | 3110 | }, |
| 3113 | else => return self.buildPointerOffset(struct_ptr, offset, .new), | 3111 | else => return func.buildPointerOffset(struct_ptr, offset, .new), |
| 3114 | } | 3112 | } |
| 3115 | } | 3113 | } |
| 3116 | | 3114 | |
| 3117 | fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3115 | fn airStructFieldVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3118 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 3116 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 3119 | const struct_field = self.air.extraData(Air.StructField, ty_pl.payload).data; | 3117 | const struct_field = func.air.extraData(Air.StructField, ty_pl.payload).data; |
| 3120 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{struct_field.struct_operand}); | 3118 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{struct_field.struct_operand}); |
| 3121 | | 3119 | |
| 3122 | const struct_ty = self.air.typeOf(struct_field.struct_operand); | 3120 | const struct_ty = func.air.typeOf(struct_field.struct_operand); |
| 3123 | const operand = try self.resolveInst(struct_field.struct_operand); | 3121 | const operand = try func.resolveInst(struct_field.struct_operand); |
| 3124 | const field_index = struct_field.field_index; | 3122 | const field_index = struct_field.field_index; |
| 3125 | const field_ty = struct_ty.structFieldType(field_index); | 3123 | const field_ty = struct_ty.structFieldType(field_index); |
| 3126 | if (!field_ty.hasRuntimeBitsIgnoreComptime()) return self.finishAir(inst, .none, &.{struct_field.struct_operand}); | 3124 | if (!field_ty.hasRuntimeBitsIgnoreComptime()) return func.finishAir(inst, .none, &.{struct_field.struct_operand}); |
| 3127 | | 3125 | |
| 3128 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(field_index, self.target)) orelse { | 3126 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(field_index, func.target)) orelse { |
| 3129 | const module = self.bin_file.base.options.module.?; | 3127 | const module = func.bin_file.base.options.module.?; |
| 3130 | return self.fail("Field type '{}' too big to fit into stack frame", .{field_ty.fmt(module)}); | 3128 | return func.fail("Field type '{}' too big to fit into stack frame", .{field_ty.fmt(module)}); |
| 3131 | }; | 3129 | }; |
| 3132 | | 3130 | |
| 3133 | const result = result: { | 3131 | const result = result: { |
| 3134 | if (isByRef(field_ty, self.target)) { | 3132 | if (isByRef(field_ty, func.target)) { |
| 3135 | switch (operand) { | 3133 | switch (operand) { |
| 3136 | .stack_offset => |stack_offset| { | 3134 | .stack_offset => |stack_offset| { |
| 3137 | break :result WValue{ .stack_offset = .{ .value = stack_offset.value + offset, .references = 1 } }; | 3135 | break :result WValue{ .stack_offset = .{ .value = stack_offset.value + offset, .references = 1 } }; |
| 3138 | }, | 3136 | }, |
| 3139 | else => break :result try self.buildPointerOffset(operand, offset, .new), | 3137 | else => break :result try func.buildPointerOffset(operand, offset, .new), |
| 3140 | } | 3138 | } |
| 3141 | } | 3139 | } |
| 3142 | | 3140 | |
| 3143 | const field = try self.load(operand, field_ty, offset); | 3141 | const field = try func.load(operand, field_ty, offset); |
| 3144 | break :result try field.toLocal(self, field_ty); | 3142 | break :result try field.toLocal(func, field_ty); |
| 3145 | }; | 3143 | }; |
| 3146 | self.finishAir(inst, result, &.{struct_field.struct_operand}); | 3144 | func.finishAir(inst, result, &.{struct_field.struct_operand}); |
| 3147 | } | 3145 | } |
| 3148 | | 3146 | |
| 3149 | fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3147 | fn airSwitchBr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3150 | // result type is always 'noreturn' | 3148 | // result type is always 'noreturn' |
| 3151 | const blocktype = wasm.block_empty; | 3149 | const blocktype = wasm.block_empty; |
| 3152 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 3150 | const pl_op = func.air.instructions.items(.data)[inst].pl_op; |
| 3153 | const target = try self.resolveInst(pl_op.operand); | 3151 | const target = try func.resolveInst(pl_op.operand); |
| 3154 | const target_ty = self.air.typeOf(pl_op.operand); | 3152 | const target_ty = func.air.typeOf(pl_op.operand); |
| 3155 | const switch_br = self.air.extraData(Air.SwitchBr, pl_op.payload); | 3153 | const switch_br = func.air.extraData(Air.SwitchBr, pl_op.payload); |
| 3156 | const liveness = try self.liveness.getSwitchBr(self.gpa, inst, switch_br.data.cases_len + 1); | 3154 | const liveness = try func.liveness.getSwitchBr(func.gpa, inst, switch_br.data.cases_len + 1); |
| 3157 | defer self.gpa.free(liveness.deaths); | 3155 | defer func.gpa.free(liveness.deaths); |
| 3158 | | 3156 | |
| 3159 | var extra_index: usize = switch_br.end; | 3157 | var extra_index: usize = switch_br.end; |
| 3160 | var case_i: u32 = 0; | 3158 | var case_i: u32 = 0; |
| ... | @@ -3164,24 +3162,24 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -3164,24 +3162,24 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3164 | var case_list = try std.ArrayList(struct { | 3162 | var case_list = try std.ArrayList(struct { |
| 3165 | values: []const CaseValue, | 3163 | values: []const CaseValue, |
| 3166 | body: []const Air.Inst.Index, | 3164 | body: []const Air.Inst.Index, |
| 3167 | }).initCapacity(self.gpa, switch_br.data.cases_len); | 3165 | }).initCapacity(func.gpa, switch_br.data.cases_len); |
| 3168 | defer for (case_list.items) |case| { | 3166 | defer for (case_list.items) |case| { |
| 3169 | self.gpa.free(case.values); | 3167 | func.gpa.free(case.values); |
| 3170 | } else case_list.deinit(); | 3168 | } else case_list.deinit(); |
| 3171 | | 3169 | |
| 3172 | var lowest_maybe: ?i32 = null; | 3170 | var lowest_maybe: ?i32 = null; |
| 3173 | var highest_maybe: ?i32 = null; | 3171 | var highest_maybe: ?i32 = null; |
| 3174 | while (case_i < switch_br.data.cases_len) : (case_i += 1) { | 3172 | while (case_i < switch_br.data.cases_len) : (case_i += 1) { |
| 3175 | const case = self.air.extraData(Air.SwitchBr.Case, extra_index); | 3173 | const case = func.air.extraData(Air.SwitchBr.Case, extra_index); |
| 3176 | const items = @ptrCast([]const Air.Inst.Ref, self.air.extra[case.end..][0..case.data.items_len]); | 3174 | const items = @ptrCast([]const Air.Inst.Ref, func.air.extra[case.end..][0..case.data.items_len]); |
| 3177 | const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len]; | 3175 | const case_body = func.air.extra[case.end + items.len ..][0..case.data.body_len]; |
| 3178 | extra_index = case.end + items.len + case_body.len; | 3176 | extra_index = case.end + items.len + case_body.len; |
| 3179 | const values = try self.gpa.alloc(CaseValue, items.len); | 3177 | const values = try func.gpa.alloc(CaseValue, items.len); |
| 3180 | errdefer self.gpa.free(values); | 3178 | errdefer func.gpa.free(values); |
| 3181 | | 3179 | |
| 3182 | for (items) |ref, i| { | 3180 | for (items) |ref, i| { |
| 3183 | const item_val = self.air.value(ref).?; | 3181 | const item_val = func.air.value(ref).?; |
| 3184 | const int_val = self.valueAsI32(item_val, target_ty); | 3182 | const int_val = func.valueAsI32(item_val, target_ty); |
| 3185 | if (lowest_maybe == null or int_val < lowest_maybe.?) { | 3183 | if (lowest_maybe == null or int_val < lowest_maybe.?) { |
| 3186 | lowest_maybe = int_val; | 3184 | lowest_maybe = int_val; |
| 3187 | } | 3185 | } |
| ... | @@ -3192,7 +3190,7 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -3192,7 +3190,7 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3192 | } | 3190 | } |
| 3193 | | 3191 | |
| 3194 | case_list.appendAssumeCapacity(.{ .values = values, .body = case_body }); | 3192 | case_list.appendAssumeCapacity(.{ .values = values, .body = case_body }); |
| 3195 | try self.startBlock(.block, blocktype); | 3193 | try func.startBlock(.block, blocktype); |
| 3196 | } | 3194 | } |
| 3197 | | 3195 | |
| 3198 | // When highest and lowest are null, we have no cases and can use a jump table | 3196 | // When highest and lowest are null, we have no cases and can use a jump table |
| ... | @@ -3203,12 +3201,12 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -3203,12 +3201,12 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3203 | // When the target is an integer size larger than u32, we have no way to use the value | 3201 | // When the target is an integer size larger than u32, we have no way to use the value |
| 3204 | // as an index, therefore we also use an if/else-chain for those cases. | 3202 | // as an index, therefore we also use an if/else-chain for those cases. |
| 3205 | // TODO: Benchmark this to find a proper value, LLVM seems to draw the line at '40~45'. | 3203 | // TODO: Benchmark this to find a proper value, LLVM seems to draw the line at '40~45'. |
| 3206 | const is_sparse = highest - lowest > 50 or target_ty.bitSize(self.target) > 32; | 3204 | const is_sparse = highest - lowest > 50 or target_ty.bitSize(func.target) > 32; |
| 3207 | | 3205 | |
| 3208 | const else_body = self.air.extra[extra_index..][0..switch_br.data.else_body_len]; | 3206 | const else_body = func.air.extra[extra_index..][0..switch_br.data.else_body_len]; |
| 3209 | const has_else_body = else_body.len != 0; | 3207 | const has_else_body = else_body.len != 0; |
| 3210 | if (has_else_body) { | 3208 | if (has_else_body) { |
| 3211 | try self.startBlock(.block, blocktype); | 3209 | try func.startBlock(.block, blocktype); |
| 3212 | } | 3210 | } |
| 3213 | | 3211 | |
| 3214 | if (!is_sparse) { | 3212 | if (!is_sparse) { |
| ... | @@ -3216,25 +3214,25 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -3216,25 +3214,25 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3216 | // The value 'target' represents the index into the table. | 3214 | // The value 'target' represents the index into the table. |
| 3217 | // Each index in the table represents a label to the branch | 3215 | // Each index in the table represents a label to the branch |
| 3218 | // to jump to. | 3216 | // to jump to. |
| 3219 | try self.startBlock(.block, blocktype); | 3217 | try func.startBlock(.block, blocktype); |
| 3220 | try self.emitWValue(target); | 3218 | try func.emitWValue(target); |
| 3221 | if (lowest < 0) { | 3219 | if (lowest < 0) { |
| 3222 | // since br_table works using indexes, starting from '0', we must ensure all values | 3220 | // since br_table works using indexes, starting from '0', we must ensure all values |
| 3223 | // we put inside, are atleast 0. | 3221 | // we put inside, are atleast 0. |
| 3224 | try self.addImm32(lowest * -1); | 3222 | try func.addImm32(lowest * -1); |
| 3225 | try self.addTag(.i32_add); | 3223 | try func.addTag(.i32_add); |
| 3226 | } else if (lowest > 0) { | 3224 | } else if (lowest > 0) { |
| 3227 | // make the index start from 0 by substracting the lowest value | 3225 | // make the index start from 0 by substracting the lowest value |
| 3228 | try self.addImm32(lowest); | 3226 | try func.addImm32(lowest); |
| 3229 | try self.addTag(.i32_sub); | 3227 | try func.addTag(.i32_sub); |
| 3230 | } | 3228 | } |
| 3231 | | 3229 | |
| 3232 | // Account for default branch so always add '1' | 3230 | // Account for default branch so always add '1' |
| 3233 | const depth = @intCast(u32, highest - lowest + @boolToInt(has_else_body)) + 1; | 3231 | const depth = @intCast(u32, highest - lowest + @boolToInt(has_else_body)) + 1; |
| 3234 | const jump_table: Mir.JumpTable = .{ .length = depth }; | 3232 | const jump_table: Mir.JumpTable = .{ .length = depth }; |
| 3235 | const table_extra_index = try self.addExtra(jump_table); | 3233 | const table_extra_index = try func.addExtra(jump_table); |
| 3236 | try self.addInst(.{ .tag = .br_table, .data = .{ .payload = table_extra_index } }); | 3234 | try func.addInst(.{ .tag = .br_table, .data = .{ .payload = table_extra_index } }); |
| 3237 | try self.mir_extra.ensureUnusedCapacity(self.gpa, depth); | 3235 | try func.mir_extra.ensureUnusedCapacity(func.gpa, depth); |
| 3238 | var value = lowest; | 3236 | var value = lowest; |
| 3239 | while (value <= highest) : (value += 1) { | 3237 | while (value <= highest) : (value += 1) { |
| 3240 | // idx represents the branch we jump to | 3238 | // idx represents the branch we jump to |
| ... | @@ -3250,11 +3248,11 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -3250,11 +3248,11 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3250 | // by using a jump table for this instead of if-else chains. | 3248 | // by using a jump table for this instead of if-else chains. |
| 3251 | break :blk if (has_else_body or target_ty.zigTypeTag() == .ErrorSet) case_i else unreachable; | 3249 | break :blk if (has_else_body or target_ty.zigTypeTag() == .ErrorSet) case_i else unreachable; |
| 3252 | }; | 3250 | }; |
| 3253 | self.mir_extra.appendAssumeCapacity(idx); | 3251 | func.mir_extra.appendAssumeCapacity(idx); |
| 3254 | } else if (has_else_body) { | 3252 | } else if (has_else_body) { |
| 3255 | self.mir_extra.appendAssumeCapacity(case_i); // default branch | 3253 | func.mir_extra.appendAssumeCapacity(case_i); // default branch |
| 3256 | } | 3254 | } |
| 3257 | try self.endBlock(); | 3255 | try func.endBlock(); |
| 3258 | } | 3256 | } |
| 3259 | | 3257 | |
| 3260 | const signedness: std.builtin.Signedness = blk: { | 3258 | const signedness: std.builtin.Signedness = blk: { |
| ... | @@ -3262,79 +3260,79 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -3262,79 +3260,79 @@ fn airSwitchBr(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 3262 | if (target_ty.zigTypeTag() != .Int) break :blk .unsigned; | 3260 | if (target_ty.zigTypeTag() != .Int) break :blk .unsigned; |
| 3263 | | 3261 | |
| 3264 | // incase of an actual integer, we emit the correct signedness | 3262 | // incase of an actual integer, we emit the correct signedness |
| 3265 | break :blk target_ty.intInfo(self.target).signedness; | 3263 | break :blk target_ty.intInfo(func.target).signedness; |
| 3266 | }; | 3264 | }; |
| 3267 | | 3265 | |
| 3268 | try self.branches.ensureUnusedCapacity(self.gpa, case_list.items.len + @boolToInt(has_else_body)); | 3266 | try func.branches.ensureUnusedCapacity(func.gpa, case_list.items.len + @boolToInt(has_else_body)); |
| 3269 | for (case_list.items) |case, index| { | 3267 | for (case_list.items) |case, index| { |
| 3270 | // when sparse, we use if/else-chain, so emit conditional checks | 3268 | // when sparse, we use if/else-chain, so emit conditional checks |
| 3271 | if (is_sparse) { | 3269 | if (is_sparse) { |
| 3272 | // for single value prong we can emit a simple if | 3270 | // for single value prong we can emit a simple if |
| 3273 | if (case.values.len == 1) { | 3271 | if (case.values.len == 1) { |
| 3274 | try self.emitWValue(target); | 3272 | try func.emitWValue(target); |
| 3275 | const val = try self.lowerConstant(case.values[0].value, target_ty); | 3273 | const val = try func.lowerConstant(case.values[0].value, target_ty); |
| 3276 | try self.emitWValue(val); | 3274 | try func.emitWValue(val); |
| 3277 | const opcode = buildOpcode(.{ | 3275 | const opcode = buildOpcode(.{ |
| 3278 | .valtype1 = typeToValtype(target_ty, self.target), | 3276 | .valtype1 = typeToValtype(target_ty, func.target), |
| 3279 | .op = .ne, // not equal, because we want to jump out of this block if it does not match the condition. | 3277 | .op = .ne, // not equal, because we want to jump out of this block if it does not match the condition. |
| 3280 | .signedness = signedness, | 3278 | .signedness = signedness, |
| 3281 | }); | 3279 | }); |
| 3282 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); | 3280 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 3283 | try self.addLabel(.br_if, 0); | 3281 | try func.addLabel(.br_if, 0); |
| 3284 | } else { | 3282 | } else { |
| 3285 | // in multi-value prongs we must check if any prongs match the target value. | 3283 | // in multi-value prongs we must check if any prongs match the target value. |
| 3286 | try self.startBlock(.block, blocktype); | 3284 | try func.startBlock(.block, blocktype); |
| 3287 | for (case.values) |value| { | 3285 | for (case.values) |value| { |
| 3288 | try self.emitWValue(target); | 3286 | try func.emitWValue(target); |
| 3289 | const val = try self.lowerConstant(value.value, target_ty); | 3287 | const val = try func.lowerConstant(value.value, target_ty); |
| 3290 | try self.emitWValue(val); | 3288 | try func.emitWValue(val); |
| 3291 | const opcode = buildOpcode(.{ | 3289 | const opcode = buildOpcode(.{ |
| 3292 | .valtype1 = typeToValtype(target_ty, self.target), | 3290 | .valtype1 = typeToValtype(target_ty, func.target), |
| 3293 | .op = .eq, | 3291 | .op = .eq, |
| 3294 | .signedness = signedness, | 3292 | .signedness = signedness, |
| 3295 | }); | 3293 | }); |
| 3296 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); | 3294 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 3297 | try self.addLabel(.br_if, 0); | 3295 | try func.addLabel(.br_if, 0); |
| 3298 | } | 3296 | } |
| 3299 | // value did not match any of the prong values | 3297 | // value did not match any of the prong values |
| 3300 | try self.addLabel(.br, 1); | 3298 | try func.addLabel(.br, 1); |
| 3301 | try self.endBlock(); | 3299 | try func.endBlock(); |
| 3302 | } | 3300 | } |
| 3303 | } | 3301 | } |
| 3304 | self.branches.appendAssumeCapacity(.{}); | 3302 | func.branches.appendAssumeCapacity(.{}); |
| 3305 | | 3303 | |
| 3306 | try self.currentBranch().values.ensureUnusedCapacity(self.gpa, liveness.deaths[index].len); | 3304 | try func.currentBranch().values.ensureUnusedCapacity(func.gpa, liveness.deaths[index].len); |
| 3307 | for (liveness.deaths[index]) |operand| { | 3305 | for (liveness.deaths[index]) |operand| { |
| 3308 | self.processDeath(Air.indexToRef(operand)); | 3306 | func.processDeath(Air.indexToRef(operand)); |
| 3309 | } | 3307 | } |
| 3310 | try self.genBody(case.body); | 3308 | try func.genBody(case.body); |
| 3311 | try self.endBlock(); | 3309 | try func.endBlock(); |
| 3312 | var case_branch = self.branches.pop(); | 3310 | var case_branch = func.branches.pop(); |
| 3313 | defer case_branch.deinit(self.gpa); | 3311 | defer case_branch.deinit(func.gpa); |
| 3314 | try self.mergeBranch(&case_branch); | 3312 | try func.mergeBranch(&case_branch); |
| 3315 | } | 3313 | } |
| 3316 | | 3314 | |
| 3317 | if (has_else_body) { | 3315 | if (has_else_body) { |
| 3318 | self.branches.appendAssumeCapacity(.{}); | 3316 | func.branches.appendAssumeCapacity(.{}); |
| 3319 | const else_deaths = liveness.deaths.len - 1; | 3317 | const else_deaths = liveness.deaths.len - 1; |
| 3320 | try self.currentBranch().values.ensureUnusedCapacity(self.gpa, liveness.deaths[else_deaths].len); | 3318 | try func.currentBranch().values.ensureUnusedCapacity(func.gpa, liveness.deaths[else_deaths].len); |
| 3321 | for (liveness.deaths[else_deaths]) |operand| { | 3319 | for (liveness.deaths[else_deaths]) |operand| { |
| 3322 | self.processDeath(Air.indexToRef(operand)); | 3320 | func.processDeath(Air.indexToRef(operand)); |
| 3323 | } | 3321 | } |
| 3324 | try self.genBody(else_body); | 3322 | try func.genBody(else_body); |
| 3325 | try self.endBlock(); | 3323 | try func.endBlock(); |
| 3326 | var else_branch = self.branches.pop(); | 3324 | var else_branch = func.branches.pop(); |
| 3327 | defer else_branch.deinit(self.gpa); | 3325 | defer else_branch.deinit(func.gpa); |
| 3328 | try self.mergeBranch(&else_branch); | 3326 | try func.mergeBranch(&else_branch); |
| 3329 | } | 3327 | } |
| 3330 | self.finishAir(inst, .none, &.{}); | 3328 | func.finishAir(inst, .none, &.{}); |
| 3331 | } | 3329 | } |
| 3332 | | 3330 | |
| 3333 | fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!void { | 3331 | fn airIsErr(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!void { |
| 3334 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 3332 | const un_op = func.air.instructions.items(.data)[inst].un_op; |
| 3335 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op}); | 3333 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{un_op}); |
| 3336 | const operand = try self.resolveInst(un_op); | 3334 | const operand = try func.resolveInst(un_op); |
| 3337 | const err_union_ty = self.air.typeOf(un_op); | 3335 | const err_union_ty = func.air.typeOf(un_op); |
| 3338 | const pl_ty = err_union_ty.errorUnionPayload(); | 3336 | const pl_ty = err_union_ty.errorUnionPayload(); |
| 3339 | | 3337 | |
| 3340 | const result = result: { | 3338 | const result = result: { |
| ... | @@ -3346,54 +3344,54 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!v | ... | @@ -3346,54 +3344,54 @@ fn airIsErr(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode) InnerError!v |
| 3346 | } | 3344 | } |
| 3347 | } | 3345 | } |
| 3348 | | 3346 | |
| 3349 | try self.emitWValue(operand); | 3347 | try func.emitWValue(operand); |
| 3350 | if (pl_ty.hasRuntimeBitsIgnoreComptime()) { | 3348 | if (pl_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3351 | try self.addMemArg(.i32_load16_u, .{ | 3349 | try func.addMemArg(.i32_load16_u, .{ |
| 3352 | .offset = operand.offset() + @intCast(u32, errUnionErrorOffset(pl_ty, self.target)), | 3350 | .offset = operand.offset() + @intCast(u32, errUnionErrorOffset(pl_ty, func.target)), |
| 3353 | .alignment = Type.anyerror.abiAlignment(self.target), | 3351 | .alignment = Type.anyerror.abiAlignment(func.target), |
| 3354 | }); | 3352 | }); |
| 3355 | } | 3353 | } |
| 3356 | | 3354 | |
| 3357 | // Compare the error value with '0' | 3355 | // Compare the error value with '0' |
| 3358 | try self.addImm32(0); | 3356 | try func.addImm32(0); |
| 3359 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); | 3357 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 3360 | | 3358 | |
| 3361 | const is_err_tmp = try self.allocLocal(Type.i32); | 3359 | const is_err_tmp = try func.allocLocal(Type.i32); |
| 3362 | try self.addLabel(.local_set, is_err_tmp.local.value); | 3360 | try func.addLabel(.local_set, is_err_tmp.local.value); |
| 3363 | break :result is_err_tmp; | 3361 | break :result is_err_tmp; |
| 3364 | }; | 3362 | }; |
| 3365 | self.finishAir(inst, result, &.{un_op}); | 3363 | func.finishAir(inst, result, &.{un_op}); |
| 3366 | } | 3364 | } |
| 3367 | | 3365 | |
| 3368 | fn airUnwrapErrUnionPayload(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void { | 3366 | fn airUnwrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void { |
| 3369 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3367 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3370 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3368 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3371 | | 3369 | |
| 3372 | const operand = try self.resolveInst(ty_op.operand); | 3370 | const operand = try func.resolveInst(ty_op.operand); |
| 3373 | const op_ty = self.air.typeOf(ty_op.operand); | 3371 | const op_ty = func.air.typeOf(ty_op.operand); |
| 3374 | const err_ty = if (op_is_ptr) op_ty.childType() else op_ty; | 3372 | const err_ty = if (op_is_ptr) op_ty.childType() else op_ty; |
| 3375 | const payload_ty = err_ty.errorUnionPayload(); | 3373 | const payload_ty = err_ty.errorUnionPayload(); |
| 3376 | | 3374 | |
| 3377 | const result = result: { | 3375 | const result = result: { |
| 3378 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) break :result WValue{ .none = {} }; | 3376 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) break :result WValue{ .none = {} }; |
| 3379 | | 3377 | |
| 3380 | const pl_offset = @intCast(u32, errUnionPayloadOffset(payload_ty, self.target)); | 3378 | const pl_offset = @intCast(u32, errUnionPayloadOffset(payload_ty, func.target)); |
| 3381 | if (op_is_ptr or isByRef(payload_ty, self.target)) { | 3379 | if (op_is_ptr or isByRef(payload_ty, func.target)) { |
| 3382 | break :result try self.buildPointerOffset(operand, pl_offset, .new); | 3380 | break :result try func.buildPointerOffset(operand, pl_offset, .new); |
| 3383 | } | 3381 | } |
| 3384 | | 3382 | |
| 3385 | const payload = try self.load(operand, payload_ty, pl_offset); | 3383 | const payload = try func.load(operand, payload_ty, pl_offset); |
| 3386 | break :result try payload.toLocal(self, payload_ty); | 3384 | break :result try payload.toLocal(func, payload_ty); |
| 3387 | }; | 3385 | }; |
| 3388 | self.finishAir(inst, result, &.{ty_op.operand}); | 3386 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3389 | } | 3387 | } |
| 3390 | | 3388 | |
| 3391 | fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void { | 3389 | fn airUnwrapErrUnionError(func: *CodeGen, inst: Air.Inst.Index, op_is_ptr: bool) InnerError!void { |
| 3392 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3390 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3393 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3391 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3394 | | 3392 | |
| 3395 | const operand = try self.resolveInst(ty_op.operand); | 3393 | const operand = try func.resolveInst(ty_op.operand); |
| 3396 | const op_ty = self.air.typeOf(ty_op.operand); | 3394 | const op_ty = func.air.typeOf(ty_op.operand); |
| 3397 | const err_ty = if (op_is_ptr) op_ty.childType() else op_ty; | 3395 | const err_ty = if (op_is_ptr) op_ty.childType() else op_ty; |
| 3398 | const payload_ty = err_ty.errorUnionPayload(); | 3396 | const payload_ty = err_ty.errorUnionPayload(); |
| 3399 | | 3397 | |
| ... | @@ -3403,94 +3401,94 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) In | ... | @@ -3403,94 +3401,94 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) In |
| 3403 | } | 3401 | } |
| 3404 | | 3402 | |
| 3405 | if (op_is_ptr or !payload_ty.hasRuntimeBitsIgnoreComptime()) { | 3403 | if (op_is_ptr or !payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3406 | break :result self.reuseOperand(ty_op.operand, operand); | 3404 | break :result func.reuseOperand(ty_op.operand, operand); |
| 3407 | } | 3405 | } |
| 3408 | | 3406 | |
| 3409 | const error_val = try self.load(operand, Type.anyerror, @intCast(u32, errUnionErrorOffset(payload_ty, self.target))); | 3407 | const error_val = try func.load(operand, Type.anyerror, @intCast(u32, errUnionErrorOffset(payload_ty, func.target))); |
| 3410 | break :result try error_val.toLocal(self, Type.anyerror); | 3408 | break :result try error_val.toLocal(func, Type.anyerror); |
| 3411 | }; | 3409 | }; |
| 3412 | self.finishAir(inst, result, &.{ty_op.operand}); | 3410 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3413 | } | 3411 | } |
| 3414 | | 3412 | |
| 3415 | fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3413 | fn airWrapErrUnionPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3416 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3414 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3417 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3415 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3418 | | 3416 | |
| 3419 | const operand = try self.resolveInst(ty_op.operand); | 3417 | const operand = try func.resolveInst(ty_op.operand); |
| 3420 | const err_ty = self.air.typeOfIndex(inst); | 3418 | const err_ty = func.air.typeOfIndex(inst); |
| 3421 | | 3419 | |
| 3422 | const pl_ty = self.air.typeOf(ty_op.operand); | 3420 | const pl_ty = func.air.typeOf(ty_op.operand); |
| 3423 | const result = result: { | 3421 | const result = result: { |
| 3424 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { | 3422 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3425 | break :result self.reuseOperand(ty_op.operand, operand); | 3423 | break :result func.reuseOperand(ty_op.operand, operand); |
| 3426 | } | 3424 | } |
| 3427 | | 3425 | |
| 3428 | const err_union = try self.allocStack(err_ty); | 3426 | const err_union = try func.allocStack(err_ty); |
| 3429 | const payload_ptr = try self.buildPointerOffset(err_union, @intCast(u32, errUnionPayloadOffset(pl_ty, self.target)), .new); | 3427 | const payload_ptr = try func.buildPointerOffset(err_union, @intCast(u32, errUnionPayloadOffset(pl_ty, func.target)), .new); |
| 3430 | try self.store(payload_ptr, operand, pl_ty, 0); | 3428 | try func.store(payload_ptr, operand, pl_ty, 0); |
| 3431 | | 3429 | |
| 3432 | // ensure we also write '0' to the error part, so any present stack value gets overwritten by it. | 3430 | // ensure we also write '0' to the error part, so any present stack value gets overwritten by it. |
| 3433 | try self.emitWValue(err_union); | 3431 | try func.emitWValue(err_union); |
| 3434 | try self.addImm32(0); | 3432 | try func.addImm32(0); |
| 3435 | const err_val_offset = @intCast(u32, errUnionErrorOffset(pl_ty, self.target)); | 3433 | const err_val_offset = @intCast(u32, errUnionErrorOffset(pl_ty, func.target)); |
| 3436 | try self.addMemArg(.i32_store16, .{ .offset = err_union.offset() + err_val_offset, .alignment = 2 }); | 3434 | try func.addMemArg(.i32_store16, .{ .offset = err_union.offset() + err_val_offset, .alignment = 2 }); |
| 3437 | break :result err_union; | 3435 | break :result err_union; |
| 3438 | }; | 3436 | }; |
| 3439 | self.finishAir(inst, result, &.{ty_op.operand}); | 3437 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3440 | } | 3438 | } |
| 3441 | | 3439 | |
| 3442 | fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3440 | fn airWrapErrUnionErr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3443 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3441 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3444 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3442 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3445 | | 3443 | |
| 3446 | const operand = try self.resolveInst(ty_op.operand); | 3444 | const operand = try func.resolveInst(ty_op.operand); |
| 3447 | const err_ty = self.air.getRefType(ty_op.ty); | 3445 | const err_ty = func.air.getRefType(ty_op.ty); |
| 3448 | const pl_ty = err_ty.errorUnionPayload(); | 3446 | const pl_ty = err_ty.errorUnionPayload(); |
| 3449 | | 3447 | |
| 3450 | const result = result: { | 3448 | const result = result: { |
| 3451 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { | 3449 | if (!pl_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3452 | break :result self.reuseOperand(ty_op.operand, operand); | 3450 | break :result func.reuseOperand(ty_op.operand, operand); |
| 3453 | } | 3451 | } |
| 3454 | | 3452 | |
| 3455 | const err_union = try self.allocStack(err_ty); | 3453 | const err_union = try func.allocStack(err_ty); |
| 3456 | // store error value | 3454 | // store error value |
| 3457 | try self.store(err_union, operand, Type.anyerror, @intCast(u32, errUnionErrorOffset(pl_ty, self.target))); | 3455 | try func.store(err_union, operand, Type.anyerror, @intCast(u32, errUnionErrorOffset(pl_ty, func.target))); |
| 3458 | | 3456 | |
| 3459 | // write 'undefined' to the payload | 3457 | // write 'undefined' to the payload |
| 3460 | const payload_ptr = try self.buildPointerOffset(err_union, @intCast(u32, errUnionPayloadOffset(pl_ty, self.target)), .new); | 3458 | const payload_ptr = try func.buildPointerOffset(err_union, @intCast(u32, errUnionPayloadOffset(pl_ty, func.target)), .new); |
| 3461 | const len = @intCast(u32, err_ty.errorUnionPayload().abiSize(self.target)); | 3459 | const len = @intCast(u32, err_ty.errorUnionPayload().abiSize(func.target)); |
| 3462 | try self.memset(payload_ptr, .{ .imm32 = len }, .{ .imm32 = 0xaaaaaaaa }); | 3460 | try func.memset(payload_ptr, .{ .imm32 = len }, .{ .imm32 = 0xaaaaaaaa }); |
| 3463 | | 3461 | |
| 3464 | break :result err_union; | 3462 | break :result err_union; |
| 3465 | }; | 3463 | }; |
| 3466 | self.finishAir(inst, result, &.{ty_op.operand}); | 3464 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3467 | } | 3465 | } |
| 3468 | | 3466 | |
| 3469 | fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3467 | fn airIntcast(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3470 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3468 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3471 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3469 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3472 | | 3470 | |
| 3473 | const ty = self.air.getRefType(ty_op.ty); | 3471 | const ty = func.air.getRefType(ty_op.ty); |
| 3474 | const operand = try self.resolveInst(ty_op.operand); | 3472 | const operand = try func.resolveInst(ty_op.operand); |
| 3475 | const operand_ty = self.air.typeOf(ty_op.operand); | 3473 | const operand_ty = func.air.typeOf(ty_op.operand); |
| 3476 | if (ty.zigTypeTag() == .Vector or operand_ty.zigTypeTag() == .Vector) { | 3474 | if (ty.zigTypeTag() == .Vector or operand_ty.zigTypeTag() == .Vector) { |
| 3477 | return self.fail("todo Wasm intcast for vectors", .{}); | 3475 | return func.fail("todo Wasm intcast for vectors", .{}); |
| 3478 | } | 3476 | } |
| 3479 | if (ty.abiSize(self.target) > 16 or operand_ty.abiSize(self.target) > 16) { | 3477 | if (ty.abiSize(func.target) > 16 or operand_ty.abiSize(func.target) > 16) { |
| 3480 | return self.fail("todo Wasm intcast for bitsize > 128", .{}); | 3478 | return func.fail("todo Wasm intcast for bitsize > 128", .{}); |
| 3481 | } | 3479 | } |
| 3482 | | 3480 | |
| 3483 | const result = try (try self.intcast(operand, operand_ty, ty)).toLocal(self, ty); | 3481 | const result = try (try func.intcast(operand, operand_ty, ty)).toLocal(func, ty); |
| 3484 | self.finishAir(inst, result, &.{}); | 3482 | func.finishAir(inst, result, &.{}); |
| 3485 | } | 3483 | } |
| 3486 | | 3484 | |
| 3487 | /// Upcasts or downcasts an integer based on the given and wanted types, | 3485 | /// Upcasts or downcasts an integer based on the given and wanted types, |
| 3488 | /// and stores the result in a new operand. | 3486 | /// and stores the result in a new operand. |
| 3489 | /// Asserts type's bitsize <= 128 | 3487 | /// Asserts type's bitsize <= 128 |
| 3490 | /// NOTE: May leave the result on the top of the stack. | 3488 | /// NOTE: May leave the result on the top of the stack. |
| 3491 | fn intcast(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!WValue { | 3489 | fn intcast(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerError!WValue { |
| 3492 | const given_info = given.intInfo(self.target); | 3490 | const given_info = given.intInfo(func.target); |
| 3493 | const wanted_info = wanted.intInfo(self.target); | 3491 | const wanted_info = wanted.intInfo(func.target); |
| 3494 | assert(given_info.bits <= 128); | 3492 | assert(given_info.bits <= 128); |
| 3495 | assert(wanted_info.bits <= 128); | 3493 | assert(wanted_info.bits <= 128); |
| 3496 | | 3494 | |
| ... | @@ -3499,463 +3497,463 @@ fn intcast(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!W | ... | @@ -3499,463 +3497,463 @@ fn intcast(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!W |
| 3499 | if (op_bits == wanted_bits) return operand; | 3497 | if (op_bits == wanted_bits) return operand; |
| 3500 | | 3498 | |
| 3501 | if (op_bits > 32 and op_bits <= 64 and wanted_bits == 32) { | 3499 | if (op_bits > 32 and op_bits <= 64 and wanted_bits == 32) { |
| 3502 | try self.emitWValue(operand); | 3500 | try func.emitWValue(operand); |
| 3503 | try self.addTag(.i32_wrap_i64); | 3501 | try func.addTag(.i32_wrap_i64); |
| 3504 | } else if (op_bits == 32 and wanted_bits > 32 and wanted_bits <= 64) { | 3502 | } else if (op_bits == 32 and wanted_bits > 32 and wanted_bits <= 64) { |
| 3505 | try self.emitWValue(operand); | 3503 | try func.emitWValue(operand); |
| 3506 | try self.addTag(switch (wanted_info.signedness) { | 3504 | try func.addTag(switch (wanted_info.signedness) { |
| 3507 | .signed => .i64_extend_i32_s, | 3505 | .signed => .i64_extend_i32_s, |
| 3508 | .unsigned => .i64_extend_i32_u, | 3506 | .unsigned => .i64_extend_i32_u, |
| 3509 | }); | 3507 | }); |
| 3510 | } else if (wanted_bits == 128) { | 3508 | } else if (wanted_bits == 128) { |
| 3511 | // for 128bit integers we store the integer in the virtual stack, rather than a local | 3509 | // for 128bit integers we store the integer in the virtual stack, rather than a local |
| 3512 | const stack_ptr = try self.allocStack(wanted); | 3510 | const stack_ptr = try func.allocStack(wanted); |
| 3513 | try self.emitWValue(stack_ptr); | 3511 | try func.emitWValue(stack_ptr); |
| 3514 | | 3512 | |
| 3515 | // for 32 bit integers, we first coerce the value into a 64 bit integer before storing it | 3513 | // for 32 bit integers, we first coerce the value into a 64 bit integer before storing it |
| 3516 | // meaning less store operations are required. | 3514 | // meaning less store operations are required. |
| 3517 | const lhs = if (op_bits == 32) blk: { | 3515 | const lhs = if (op_bits == 32) blk: { |
| 3518 | break :blk try self.intcast(operand, given, if (wanted.isSignedInt()) Type.i64 else Type.u64); | 3516 | break :blk try func.intcast(operand, given, if (wanted.isSignedInt()) Type.i64 else Type.u64); |
| 3519 | } else operand; | 3517 | } else operand; |
| 3520 | | 3518 | |
| 3521 | // store msb first | 3519 | // store msb first |
| 3522 | try self.store(.{ .stack = {} }, lhs, Type.u64, 0 + stack_ptr.offset()); | 3520 | try func.store(.{ .stack = {} }, lhs, Type.u64, 0 + stack_ptr.offset()); |
| 3523 | | 3521 | |
| 3524 | // For signed integers we shift msb by 63 (64bit integer - 1 sign bit) and store remaining value | 3522 | // For signed integers we shift msb by 63 (64bit integer - 1 sign bit) and store remaining value |
| 3525 | if (wanted.isSignedInt()) { | 3523 | if (wanted.isSignedInt()) { |
| 3526 | try self.emitWValue(stack_ptr); | 3524 | try func.emitWValue(stack_ptr); |
| 3527 | const shr = try self.binOp(lhs, .{ .imm64 = 63 }, Type.i64, .shr); | 3525 | const shr = try func.binOp(lhs, .{ .imm64 = 63 }, Type.i64, .shr); |
| 3528 | try self.store(.{ .stack = {} }, shr, Type.u64, 8 + stack_ptr.offset()); | 3526 | try func.store(.{ .stack = {} }, shr, Type.u64, 8 + stack_ptr.offset()); |
| 3529 | } else { | 3527 | } else { |
| 3530 | // Ensure memory of lsb is zero'd | 3528 | // Ensure memory of lsb is zero'd |
| 3531 | try self.store(stack_ptr, .{ .imm64 = 0 }, Type.u64, 8); | 3529 | try func.store(stack_ptr, .{ .imm64 = 0 }, Type.u64, 8); |
| 3532 | } | 3530 | } |
| 3533 | return stack_ptr; | 3531 | return stack_ptr; |
| 3534 | } else return self.load(operand, wanted, 0); | 3532 | } else return func.load(operand, wanted, 0); |
| 3535 | | 3533 | |
| 3536 | return WValue{ .stack = {} }; | 3534 | return WValue{ .stack = {} }; |
| 3537 | } | 3535 | } |
| 3538 | | 3536 | |
| 3539 | fn airIsNull(self: *Self, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!void { | 3537 | fn airIsNull(func: *CodeGen, inst: Air.Inst.Index, opcode: wasm.Opcode, op_kind: enum { value, ptr }) InnerError!void { |
| 3540 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 3538 | const un_op = func.air.instructions.items(.data)[inst].un_op; |
| 3541 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op}); | 3539 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{un_op}); |
| 3542 | const operand = try self.resolveInst(un_op); | 3540 | const operand = try func.resolveInst(un_op); |
| 3543 | | 3541 | |
| 3544 | const op_ty = self.air.typeOf(un_op); | 3542 | const op_ty = func.air.typeOf(un_op); |
| 3545 | const optional_ty = if (op_kind == .ptr) op_ty.childType() else op_ty; | 3543 | const optional_ty = if (op_kind == .ptr) op_ty.childType() else op_ty; |
| 3546 | const is_null = try self.isNull(operand, optional_ty, opcode); | 3544 | const is_null = try func.isNull(operand, optional_ty, opcode); |
| 3547 | const result = try is_null.toLocal(self, optional_ty); | 3545 | const result = try is_null.toLocal(func, optional_ty); |
| 3548 | self.finishAir(inst, result, &.{un_op}); | 3546 | func.finishAir(inst, result, &.{un_op}); |
| 3549 | } | 3547 | } |
| 3550 | | 3548 | |
| 3551 | /// For a given type and operand, checks if it's considered `null`. | 3549 | /// For a given type and operand, checks if it's considered `null`. |
| 3552 | /// NOTE: Leaves the result on the stack | 3550 | /// NOTE: Leaves the result on the stack |
| 3553 | fn isNull(self: *Self, operand: WValue, optional_ty: Type, opcode: wasm.Opcode) InnerError!WValue { | 3551 | fn isNull(func: *CodeGen, operand: WValue, optional_ty: Type, opcode: wasm.Opcode) InnerError!WValue { |
| 3554 | try self.emitWValue(operand); | 3552 | try func.emitWValue(operand); |
| 3555 | if (!optional_ty.optionalReprIsPayload()) { | 3553 | if (!optional_ty.optionalReprIsPayload()) { |
| 3556 | var buf: Type.Payload.ElemType = undefined; | 3554 | var buf: Type.Payload.ElemType = undefined; |
| 3557 | const payload_ty = optional_ty.optionalChild(&buf); | 3555 | const payload_ty = optional_ty.optionalChild(&buf); |
| 3558 | // When payload is zero-bits, we can treat operand as a value, rather than | 3556 | // When payload is zero-bits, we can treat operand as a value, rather than |
| 3559 | // a pointer to the stack value | 3557 | // a pointer to the stack value |
| 3560 | if (payload_ty.hasRuntimeBitsIgnoreComptime()) { | 3558 | if (payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3561 | try self.addMemArg(.i32_load8_u, .{ .offset = operand.offset(), .alignment = 1 }); | 3559 | try func.addMemArg(.i32_load8_u, .{ .offset = operand.offset(), .alignment = 1 }); |
| 3562 | } | 3560 | } |
| 3563 | } | 3561 | } |
| 3564 | | 3562 | |
| 3565 | // Compare the null value with '0' | 3563 | // Compare the null value with '0' |
| 3566 | try self.addImm32(0); | 3564 | try func.addImm32(0); |
| 3567 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); | 3565 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 3568 | | 3566 | |
| 3569 | return WValue{ .stack = {} }; | 3567 | return WValue{ .stack = {} }; |
| 3570 | } | 3568 | } |
| 3571 | | 3569 | |
| 3572 | fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3570 | fn airOptionalPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3573 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3571 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3574 | const opt_ty = self.air.typeOf(ty_op.operand); | 3572 | const opt_ty = func.air.typeOf(ty_op.operand); |
| 3575 | const payload_ty = self.air.typeOfIndex(inst); | 3573 | const payload_ty = func.air.typeOfIndex(inst); |
| 3576 | if (self.liveness.isUnused(inst) or !payload_ty.hasRuntimeBitsIgnoreComptime()) { | 3574 | if (func.liveness.isUnused(inst) or !payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3577 | return self.finishAir(inst, .none, &.{ty_op.operand}); | 3575 | return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3578 | } | 3576 | } |
| 3579 | | 3577 | |
| 3580 | const result = result: { | 3578 | const result = result: { |
| 3581 | const operand = try self.resolveInst(ty_op.operand); | 3579 | const operand = try func.resolveInst(ty_op.operand); |
| 3582 | if (opt_ty.optionalReprIsPayload()) break :result self.reuseOperand(ty_op.operand, operand); | 3580 | if (opt_ty.optionalReprIsPayload()) break :result func.reuseOperand(ty_op.operand, operand); |
| 3583 | | 3581 | |
| 3584 | const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target); | 3582 | const offset = opt_ty.abiSize(func.target) - payload_ty.abiSize(func.target); |
| 3585 | | 3583 | |
| 3586 | if (isByRef(payload_ty, self.target)) { | 3584 | if (isByRef(payload_ty, func.target)) { |
| 3587 | break :result try self.buildPointerOffset(operand, offset, .new); | 3585 | break :result try func.buildPointerOffset(operand, offset, .new); |
| 3588 | } | 3586 | } |
| 3589 | | 3587 | |
| 3590 | const payload = try self.load(operand, payload_ty, @intCast(u32, offset)); | 3588 | const payload = try func.load(operand, payload_ty, @intCast(u32, offset)); |
| 3591 | break :result try payload.toLocal(self, payload_ty); | 3589 | break :result try payload.toLocal(func, payload_ty); |
| 3592 | }; | 3590 | }; |
| 3593 | self.finishAir(inst, result, &.{ty_op.operand}); | 3591 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3594 | } | 3592 | } |
| 3595 | | 3593 | |
| 3596 | fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3594 | fn airOptionalPayloadPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3597 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3595 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3598 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3596 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3599 | const operand = try self.resolveInst(ty_op.operand); | 3597 | const operand = try func.resolveInst(ty_op.operand); |
| 3600 | const opt_ty = self.air.typeOf(ty_op.operand).childType(); | 3598 | const opt_ty = func.air.typeOf(ty_op.operand).childType(); |
| 3601 | | 3599 | |
| 3602 | const result = result: { | 3600 | const result = result: { |
| 3603 | var buf: Type.Payload.ElemType = undefined; | 3601 | var buf: Type.Payload.ElemType = undefined; |
| 3604 | const payload_ty = opt_ty.optionalChild(&buf); | 3602 | const payload_ty = opt_ty.optionalChild(&buf); |
| 3605 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or opt_ty.optionalReprIsPayload()) { | 3603 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or opt_ty.optionalReprIsPayload()) { |
| 3606 | break :result self.reuseOperand(ty_op.operand, operand); | 3604 | break :result func.reuseOperand(ty_op.operand, operand); |
| 3607 | } | 3605 | } |
| 3608 | | 3606 | |
| 3609 | const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target); | 3607 | const offset = opt_ty.abiSize(func.target) - payload_ty.abiSize(func.target); |
| 3610 | break :result try self.buildPointerOffset(operand, offset, .new); | 3608 | break :result try func.buildPointerOffset(operand, offset, .new); |
| 3611 | }; | 3609 | }; |
| 3612 | self.finishAir(inst, result, &.{ty_op.operand}); | 3610 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3613 | } | 3611 | } |
| 3614 | | 3612 | |
| 3615 | fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3613 | fn airOptionalPayloadPtrSet(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3616 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3614 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3617 | const operand = try self.resolveInst(ty_op.operand); | 3615 | const operand = try func.resolveInst(ty_op.operand); |
| 3618 | const opt_ty = self.air.typeOf(ty_op.operand).childType(); | 3616 | const opt_ty = func.air.typeOf(ty_op.operand).childType(); |
| 3619 | var buf: Type.Payload.ElemType = undefined; | 3617 | var buf: Type.Payload.ElemType = undefined; |
| 3620 | const payload_ty = opt_ty.optionalChild(&buf); | 3618 | const payload_ty = opt_ty.optionalChild(&buf); |
| 3621 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | 3619 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3622 | return self.fail("TODO: Implement OptionalPayloadPtrSet for optional with zero-sized type {}", .{payload_ty.fmtDebug()}); | 3620 | return func.fail("TODO: Implement OptionalPayloadPtrSet for optional with zero-sized type {}", .{payload_ty.fmtDebug()}); |
| 3623 | } | 3621 | } |
| 3624 | | 3622 | |
| 3625 | if (opt_ty.optionalReprIsPayload()) { | 3623 | if (opt_ty.optionalReprIsPayload()) { |
| 3626 | return self.finishAir(inst, operand, &.{ty_op.operand}); | 3624 | return func.finishAir(inst, operand, &.{ty_op.operand}); |
| 3627 | } | 3625 | } |
| 3628 | | 3626 | |
| 3629 | const offset = std.math.cast(u32, opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target)) orelse { | 3627 | const offset = std.math.cast(u32, opt_ty.abiSize(func.target) - payload_ty.abiSize(func.target)) orelse { |
| 3630 | const module = self.bin_file.base.options.module.?; | 3628 | const module = func.bin_file.base.options.module.?; |
| 3631 | return self.fail("Optional type {} too big to fit into stack frame", .{opt_ty.fmt(module)}); | 3629 | return func.fail("Optional type {} too big to fit into stack frame", .{opt_ty.fmt(module)}); |
| 3632 | }; | 3630 | }; |
| 3633 | | 3631 | |
| 3634 | try self.emitWValue(operand); | 3632 | try func.emitWValue(operand); |
| 3635 | try self.addImm32(1); | 3633 | try func.addImm32(1); |
| 3636 | try self.addMemArg(.i32_store8, .{ .offset = operand.offset(), .alignment = 1 }); | 3634 | try func.addMemArg(.i32_store8, .{ .offset = operand.offset(), .alignment = 1 }); |
| 3637 | | 3635 | |
| 3638 | const result = try self.buildPointerOffset(operand, offset, .new); | 3636 | const result = try func.buildPointerOffset(operand, offset, .new); |
| 3639 | return self.finishAir(inst, result, &.{ty_op.operand}); | 3637 | return func.finishAir(inst, result, &.{ty_op.operand}); |
| 3640 | } | 3638 | } |
| 3641 | | 3639 | |
| 3642 | fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3640 | fn airWrapOptional(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3643 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3641 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3644 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3642 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3645 | const payload_ty = self.air.typeOf(ty_op.operand); | 3643 | const payload_ty = func.air.typeOf(ty_op.operand); |
| 3646 | | 3644 | |
| 3647 | const result = result: { | 3645 | const result = result: { |
| 3648 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | 3646 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3649 | const non_null_bit = try self.allocStack(Type.initTag(.u1)); | 3647 | const non_null_bit = try func.allocStack(Type.initTag(.u1)); |
| 3650 | try self.emitWValue(non_null_bit); | 3648 | try func.emitWValue(non_null_bit); |
| 3651 | try self.addImm32(1); | 3649 | try func.addImm32(1); |
| 3652 | try self.addMemArg(.i32_store8, .{ .offset = non_null_bit.offset(), .alignment = 1 }); | 3650 | try func.addMemArg(.i32_store8, .{ .offset = non_null_bit.offset(), .alignment = 1 }); |
| 3653 | break :result non_null_bit; | 3651 | break :result non_null_bit; |
| 3654 | } | 3652 | } |
| 3655 | | 3653 | |
| 3656 | const operand = try self.resolveInst(ty_op.operand); | 3654 | const operand = try func.resolveInst(ty_op.operand); |
| 3657 | const op_ty = self.air.typeOfIndex(inst); | 3655 | const op_ty = func.air.typeOfIndex(inst); |
| 3658 | if (op_ty.optionalReprIsPayload()) { | 3656 | if (op_ty.optionalReprIsPayload()) { |
| 3659 | break :result self.reuseOperand(ty_op.operand, operand); | 3657 | break :result func.reuseOperand(ty_op.operand, operand); |
| 3660 | } | 3658 | } |
| 3661 | const offset = std.math.cast(u32, op_ty.abiSize(self.target) - payload_ty.abiSize(self.target)) orelse { | 3659 | const offset = std.math.cast(u32, op_ty.abiSize(func.target) - payload_ty.abiSize(func.target)) orelse { |
| 3662 | const module = self.bin_file.base.options.module.?; | 3660 | const module = func.bin_file.base.options.module.?; |
| 3663 | return self.fail("Optional type {} too big to fit into stack frame", .{op_ty.fmt(module)}); | 3661 | return func.fail("Optional type {} too big to fit into stack frame", .{op_ty.fmt(module)}); |
| 3664 | }; | 3662 | }; |
| 3665 | | 3663 | |
| 3666 | // Create optional type, set the non-null bit, and store the operand inside the optional type | 3664 | // Create optional type, set the non-null bit, and store the operand inside the optional type |
| 3667 | const result_ptr = try self.allocStack(op_ty); | 3665 | const result_ptr = try func.allocStack(op_ty); |
| 3668 | try self.emitWValue(result_ptr); | 3666 | try func.emitWValue(result_ptr); |
| 3669 | try self.addImm32(1); | 3667 | try func.addImm32(1); |
| 3670 | try self.addMemArg(.i32_store8, .{ .offset = result_ptr.offset(), .alignment = 1 }); | 3668 | try func.addMemArg(.i32_store8, .{ .offset = result_ptr.offset(), .alignment = 1 }); |
| 3671 | | 3669 | |
| 3672 | const payload_ptr = try self.buildPointerOffset(result_ptr, offset, .new); | 3670 | const payload_ptr = try func.buildPointerOffset(result_ptr, offset, .new); |
| 3673 | try self.store(payload_ptr, operand, payload_ty, 0); | 3671 | try func.store(payload_ptr, operand, payload_ty, 0); |
| 3674 | break :result result_ptr; | 3672 | break :result result_ptr; |
| 3675 | }; | 3673 | }; |
| 3676 | | 3674 | |
| 3677 | self.finishAir(inst, result, &.{ty_op.operand}); | 3675 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3678 | } | 3676 | } |
| 3679 | | 3677 | |
| 3680 | fn airSlice(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3678 | fn airSlice(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3681 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 3679 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 3682 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; | 3680 | const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3683 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 3681 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 3684 | | 3682 | |
| 3685 | const lhs = try self.resolveInst(bin_op.lhs); | 3683 | const lhs = try func.resolveInst(bin_op.lhs); |
| 3686 | const rhs = try self.resolveInst(bin_op.rhs); | 3684 | const rhs = try func.resolveInst(bin_op.rhs); |
| 3687 | const slice_ty = self.air.typeOfIndex(inst); | 3685 | const slice_ty = func.air.typeOfIndex(inst); |
| 3688 | | 3686 | |
| 3689 | const slice = try self.allocStack(slice_ty); | 3687 | const slice = try func.allocStack(slice_ty); |
| 3690 | try self.store(slice, lhs, Type.usize, 0); | 3688 | try func.store(slice, lhs, Type.usize, 0); |
| 3691 | try self.store(slice, rhs, Type.usize, self.ptrSize()); | 3689 | try func.store(slice, rhs, Type.usize, func.ptrSize()); |
| 3692 | | 3690 | |
| 3693 | self.finishAir(inst, slice, &.{ bin_op.lhs, bin_op.rhs }); | 3691 | func.finishAir(inst, slice, &.{ bin_op.lhs, bin_op.rhs }); |
| 3694 | } | 3692 | } |
| 3695 | | 3693 | |
| 3696 | fn airSliceLen(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3694 | fn airSliceLen(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3697 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3695 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3698 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3696 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3699 | | 3697 | |
| 3700 | const operand = try self.resolveInst(ty_op.operand); | 3698 | const operand = try func.resolveInst(ty_op.operand); |
| 3701 | const len = try self.load(operand, Type.usize, self.ptrSize()); | 3699 | const len = try func.load(operand, Type.usize, func.ptrSize()); |
| 3702 | const result = try len.toLocal(self, Type.usize); | 3700 | const result = try len.toLocal(func, Type.usize); |
| 3703 | self.finishAir(inst, result, &.{ty_op.operand}); | 3701 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3704 | } | 3702 | } |
| 3705 | | 3703 | |
| 3706 | fn airSliceElemVal(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3704 | fn airSliceElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3707 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 3705 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 3708 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 3706 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 3709 | | 3707 | |
| 3710 | const slice_ty = self.air.typeOf(bin_op.lhs); | 3708 | const slice_ty = func.air.typeOf(bin_op.lhs); |
| 3711 | const slice = try self.resolveInst(bin_op.lhs); | 3709 | const slice = try func.resolveInst(bin_op.lhs); |
| 3712 | const index = try self.resolveInst(bin_op.rhs); | 3710 | const index = try func.resolveInst(bin_op.rhs); |
| 3713 | const elem_ty = slice_ty.childType(); | 3711 | const elem_ty = slice_ty.childType(); |
| 3714 | const elem_size = elem_ty.abiSize(self.target); | 3712 | const elem_size = elem_ty.abiSize(func.target); |
| 3715 | | 3713 | |
| 3716 | // load pointer onto stack | 3714 | // load pointer onto stack |
| 3717 | _ = try self.load(slice, Type.usize, 0); | 3715 | _ = try func.load(slice, Type.usize, 0); |
| 3718 | | 3716 | |
| 3719 | // calculate index into slice | 3717 | // calculate index into slice |
| 3720 | try self.emitWValue(index); | 3718 | try func.emitWValue(index); |
| 3721 | try self.addImm32(@bitCast(i32, @intCast(u32, elem_size))); | 3719 | try func.addImm32(@bitCast(i32, @intCast(u32, elem_size))); |
| 3722 | try self.addTag(.i32_mul); | 3720 | try func.addTag(.i32_mul); |
| 3723 | try self.addTag(.i32_add); | 3721 | try func.addTag(.i32_add); |
| 3724 | | 3722 | |
| 3725 | const result_ptr = try self.allocLocal(elem_ty); | 3723 | const result_ptr = try func.allocLocal(elem_ty); |
| 3726 | try self.addLabel(.local_set, result_ptr.local.value); | 3724 | try func.addLabel(.local_set, result_ptr.local.value); |
| 3727 | | 3725 | |
| 3728 | const result = if (!isByRef(elem_ty, self.target)) result: { | 3726 | const result = if (!isByRef(elem_ty, func.target)) result: { |
| 3729 | const elem_val = try self.load(result_ptr, elem_ty, 0); | 3727 | const elem_val = try func.load(result_ptr, elem_ty, 0); |
| 3730 | break :result try elem_val.toLocal(self, elem_ty); | 3728 | break :result try elem_val.toLocal(func, elem_ty); |
| 3731 | } else result_ptr; | 3729 | } else result_ptr; |
| 3732 | | 3730 | |
| 3733 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 3731 | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 3734 | } | 3732 | } |
| 3735 | | 3733 | |
| 3736 | fn airSliceElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3734 | fn airSliceElemPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3737 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 3735 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 3738 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; | 3736 | const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3739 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 3737 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 3740 | | 3738 | |
| 3741 | const elem_ty = self.air.getRefType(ty_pl.ty).childType(); | 3739 | const elem_ty = func.air.getRefType(ty_pl.ty).childType(); |
| 3742 | const elem_size = elem_ty.abiSize(self.target); | 3740 | const elem_size = elem_ty.abiSize(func.target); |
| 3743 | | 3741 | |
| 3744 | const slice = try self.resolveInst(bin_op.lhs); | 3742 | const slice = try func.resolveInst(bin_op.lhs); |
| 3745 | const index = try self.resolveInst(bin_op.rhs); | 3743 | const index = try func.resolveInst(bin_op.rhs); |
| 3746 | | 3744 | |
| 3747 | _ = try self.load(slice, Type.usize, 0); | 3745 | _ = try func.load(slice, Type.usize, 0); |
| 3748 | | 3746 | |
| 3749 | // calculate index into slice | 3747 | // calculate index into slice |
| 3750 | try self.emitWValue(index); | 3748 | try func.emitWValue(index); |
| 3751 | try self.addImm32(@bitCast(i32, @intCast(u32, elem_size))); | 3749 | try func.addImm32(@bitCast(i32, @intCast(u32, elem_size))); |
| 3752 | try self.addTag(.i32_mul); | 3750 | try func.addTag(.i32_mul); |
| 3753 | try self.addTag(.i32_add); | 3751 | try func.addTag(.i32_add); |
| 3754 | | 3752 | |
| 3755 | const result = try self.allocLocal(Type.i32); | 3753 | const result = try func.allocLocal(Type.i32); |
| 3756 | try self.addLabel(.local_set, result.local.value); | 3754 | try func.addLabel(.local_set, result.local.value); |
| 3757 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 3755 | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 3758 | } | 3756 | } |
| 3759 | | 3757 | |
| 3760 | fn airSlicePtr(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3758 | fn airSlicePtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3761 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3759 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3762 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3760 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3763 | const operand = try self.resolveInst(ty_op.operand); | 3761 | const operand = try func.resolveInst(ty_op.operand); |
| 3764 | const ptr = try self.load(operand, Type.usize, 0); | 3762 | const ptr = try func.load(operand, Type.usize, 0); |
| 3765 | const result = try ptr.toLocal(self, Type.usize); | 3763 | const result = try ptr.toLocal(func, Type.usize); |
| 3766 | self.finishAir(inst, result, &.{ty_op.operand}); | 3764 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 3767 | } | 3765 | } |
| 3768 | | 3766 | |
| 3769 | fn airTrunc(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3767 | fn airTrunc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3770 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3768 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3771 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3769 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3772 | | 3770 | |
| 3773 | const operand = try self.resolveInst(ty_op.operand); | 3771 | const operand = try func.resolveInst(ty_op.operand); |
| 3774 | const wanted_ty = self.air.getRefType(ty_op.ty); | 3772 | const wanted_ty = func.air.getRefType(ty_op.ty); |
| 3775 | const op_ty = self.air.typeOf(ty_op.operand); | 3773 | const op_ty = func.air.typeOf(ty_op.operand); |
| 3776 | | 3774 | |
| 3777 | const int_info = op_ty.intInfo(self.target); | 3775 | const int_info = op_ty.intInfo(func.target); |
| 3778 | if (toWasmBits(int_info.bits) == null) { | 3776 | if (toWasmBits(int_info.bits) == null) { |
| 3779 | return self.fail("TODO: Implement wasm integer truncation for integer bitsize: {d}", .{int_info.bits}); | 3777 | return func.fail("TODO: Implement wasm integer truncation for integer bitsize: {d}", .{int_info.bits}); |
| 3780 | } | 3778 | } |
| 3781 | | 3779 | |
| 3782 | var result = try self.intcast(operand, op_ty, wanted_ty); | 3780 | var result = try func.intcast(operand, op_ty, wanted_ty); |
| 3783 | const wanted_bits = wanted_ty.intInfo(self.target).bits; | 3781 | const wanted_bits = wanted_ty.intInfo(func.target).bits; |
| 3784 | const wasm_bits = toWasmBits(wanted_bits).?; | 3782 | const wasm_bits = toWasmBits(wanted_bits).?; |
| 3785 | if (wasm_bits != wanted_bits) { | 3783 | if (wasm_bits != wanted_bits) { |
| 3786 | result = try self.wrapOperand(result, wanted_ty); | 3784 | result = try func.wrapOperand(result, wanted_ty); |
| 3787 | } | 3785 | } |
| 3788 | | 3786 | |
| 3789 | self.finishAir(inst, try result.toLocal(self, wanted_ty), &.{ty_op.operand}); | 3787 | func.finishAir(inst, try result.toLocal(func, wanted_ty), &.{ty_op.operand}); |
| 3790 | } | 3788 | } |
| 3791 | | 3789 | |
| 3792 | fn airBoolToInt(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3790 | fn airBoolToInt(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3793 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 3791 | const un_op = func.air.instructions.items(.data)[inst].un_op; |
| 3794 | const result = if (self.liveness.isUnused(inst)) | 3792 | const result = if (func.liveness.isUnused(inst)) |
| 3795 | WValue{ .none = {} } | 3793 | WValue{ .none = {} } |
| 3796 | else result: { | 3794 | else result: { |
| 3797 | const operand = try self.resolveInst(un_op); | 3795 | const operand = try func.resolveInst(un_op); |
| 3798 | break :result self.reuseOperand(un_op, operand); | 3796 | break :result func.reuseOperand(un_op, operand); |
| 3799 | }; | 3797 | }; |
| 3800 | | 3798 | |
| 3801 | self.finishAir(inst, result, &.{un_op}); | 3799 | func.finishAir(inst, result, &.{un_op}); |
| 3802 | } | 3800 | } |
| 3803 | | 3801 | |
| 3804 | fn airArrayToSlice(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3802 | fn airArrayToSlice(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3805 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 3803 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 3806 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 3804 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 3807 | | 3805 | |
| 3808 | const operand = try self.resolveInst(ty_op.operand); | 3806 | const operand = try func.resolveInst(ty_op.operand); |
| 3809 | const array_ty = self.air.typeOf(ty_op.operand).childType(); | 3807 | const array_ty = func.air.typeOf(ty_op.operand).childType(); |
| 3810 | const slice_ty = self.air.getRefType(ty_op.ty); | 3808 | const slice_ty = func.air.getRefType(ty_op.ty); |
| 3811 | | 3809 | |
| 3812 | // create a slice on the stack | 3810 | // create a slice on the stack |
| 3813 | const slice_local = try self.allocStack(slice_ty); | 3811 | const slice_local = try func.allocStack(slice_ty); |
| 3814 | | 3812 | |
| 3815 | // store the array ptr in the slice | 3813 | // store the array ptr in the slice |
| 3816 | if (array_ty.hasRuntimeBitsIgnoreComptime()) { | 3814 | if (array_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3817 | try self.store(slice_local, operand, Type.usize, 0); | 3815 | try func.store(slice_local, operand, Type.usize, 0); |
| 3818 | } | 3816 | } |
| 3819 | | 3817 | |
| 3820 | // store the length of the array in the slice | 3818 | // store the length of the array in the slice |
| 3821 | const len = WValue{ .imm32 = @intCast(u32, array_ty.arrayLen()) }; | 3819 | const len = WValue{ .imm32 = @intCast(u32, array_ty.arrayLen()) }; |
| 3822 | try self.store(slice_local, len, Type.usize, self.ptrSize()); | 3820 | try func.store(slice_local, len, Type.usize, func.ptrSize()); |
| 3823 | | 3821 | |
| 3824 | self.finishAir(inst, slice_local, &.{ty_op.operand}); | 3822 | func.finishAir(inst, slice_local, &.{ty_op.operand}); |
| 3825 | } | 3823 | } |
| 3826 | | 3824 | |
| 3827 | fn airPtrToInt(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3825 | fn airPtrToInt(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3828 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 3826 | const un_op = func.air.instructions.items(.data)[inst].un_op; |
| 3829 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op}); | 3827 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{un_op}); |
| 3830 | const operand = try self.resolveInst(un_op); | 3828 | const operand = try func.resolveInst(un_op); |
| 3831 | | 3829 | |
| 3832 | const result = switch (operand) { | 3830 | const result = switch (operand) { |
| 3833 | // for stack offset, return a pointer to this offset. | 3831 | // for stack offset, return a pointer to this offset. |
| 3834 | .stack_offset => try self.buildPointerOffset(operand, 0, .new), | 3832 | .stack_offset => try func.buildPointerOffset(operand, 0, .new), |
| 3835 | else => self.reuseOperand(un_op, operand), | 3833 | else => func.reuseOperand(un_op, operand), |
| 3836 | }; | 3834 | }; |
| 3837 | self.finishAir(inst, result, &.{un_op}); | 3835 | func.finishAir(inst, result, &.{un_op}); |
| 3838 | } | 3836 | } |
| 3839 | | 3837 | |
| 3840 | fn airPtrElemVal(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3838 | fn airPtrElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3841 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 3839 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 3842 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 3840 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 3843 | | 3841 | |
| 3844 | const ptr_ty = self.air.typeOf(bin_op.lhs); | 3842 | const ptr_ty = func.air.typeOf(bin_op.lhs); |
| 3845 | const ptr = try self.resolveInst(bin_op.lhs); | 3843 | const ptr = try func.resolveInst(bin_op.lhs); |
| 3846 | const index = try self.resolveInst(bin_op.rhs); | 3844 | const index = try func.resolveInst(bin_op.rhs); |
| 3847 | const elem_ty = ptr_ty.childType(); | 3845 | const elem_ty = ptr_ty.childType(); |
| 3848 | const elem_size = elem_ty.abiSize(self.target); | 3846 | const elem_size = elem_ty.abiSize(func.target); |
| 3849 | | 3847 | |
| 3850 | // load pointer onto the stack | 3848 | // load pointer onto the stack |
| 3851 | if (ptr_ty.isSlice()) { | 3849 | if (ptr_ty.isSlice()) { |
| 3852 | _ = try self.load(ptr, Type.usize, 0); | 3850 | _ = try func.load(ptr, Type.usize, 0); |
| 3853 | } else { | 3851 | } else { |
| 3854 | try self.lowerToStack(ptr); | 3852 | try func.lowerToStack(ptr); |
| 3855 | } | 3853 | } |
| 3856 | | 3854 | |
| 3857 | // calculate index into slice | 3855 | // calculate index into slice |
| 3858 | try self.emitWValue(index); | 3856 | try func.emitWValue(index); |
| 3859 | try self.addImm32(@bitCast(i32, @intCast(u32, elem_size))); | 3857 | try func.addImm32(@bitCast(i32, @intCast(u32, elem_size))); |
| 3860 | try self.addTag(.i32_mul); | 3858 | try func.addTag(.i32_mul); |
| 3861 | try self.addTag(.i32_add); | 3859 | try func.addTag(.i32_add); |
| 3862 | | 3860 | |
| 3863 | const elem_result = val: { | 3861 | const elem_result = val: { |
| 3864 | var result = try self.allocLocal(elem_ty); | 3862 | var result = try func.allocLocal(elem_ty); |
| 3865 | try self.addLabel(.local_set, result.local.value); | 3863 | try func.addLabel(.local_set, result.local.value); |
| 3866 | if (isByRef(elem_ty, self.target)) { | 3864 | if (isByRef(elem_ty, func.target)) { |
| 3867 | break :val result; | 3865 | break :val result; |
| 3868 | } | 3866 | } |
| 3869 | defer result.free(self); // only free if it's not returned like above | 3867 | defer result.free(func); // only free if it's not returned like above |
| 3870 | | 3868 | |
| 3871 | const elem_val = try self.load(result, elem_ty, 0); | 3869 | const elem_val = try func.load(result, elem_ty, 0); |
| 3872 | break :val try elem_val.toLocal(self, elem_ty); | 3870 | break :val try elem_val.toLocal(func, elem_ty); |
| 3873 | }; | 3871 | }; |
| 3874 | self.finishAir(inst, elem_result, &.{ bin_op.lhs, bin_op.rhs }); | 3872 | func.finishAir(inst, elem_result, &.{ bin_op.lhs, bin_op.rhs }); |
| 3875 | } | 3873 | } |
| 3876 | | 3874 | |
| 3877 | fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3875 | fn airPtrElemPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3878 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 3876 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 3879 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; | 3877 | const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3880 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 3878 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 3881 | | 3879 | |
| 3882 | const ptr_ty = self.air.typeOf(bin_op.lhs); | 3880 | const ptr_ty = func.air.typeOf(bin_op.lhs); |
| 3883 | const elem_ty = self.air.getRefType(ty_pl.ty).childType(); | 3881 | const elem_ty = func.air.getRefType(ty_pl.ty).childType(); |
| 3884 | const elem_size = elem_ty.abiSize(self.target); | 3882 | const elem_size = elem_ty.abiSize(func.target); |
| 3885 | | 3883 | |
| 3886 | const ptr = try self.resolveInst(bin_op.lhs); | 3884 | const ptr = try func.resolveInst(bin_op.lhs); |
| 3887 | const index = try self.resolveInst(bin_op.rhs); | 3885 | const index = try func.resolveInst(bin_op.rhs); |
| 3888 | | 3886 | |
| 3889 | // load pointer onto the stack | 3887 | // load pointer onto the stack |
| 3890 | if (ptr_ty.isSlice()) { | 3888 | if (ptr_ty.isSlice()) { |
| 3891 | _ = try self.load(ptr, Type.usize, 0); | 3889 | _ = try func.load(ptr, Type.usize, 0); |
| 3892 | } else { | 3890 | } else { |
| 3893 | try self.lowerToStack(ptr); | 3891 | try func.lowerToStack(ptr); |
| 3894 | } | 3892 | } |
| 3895 | | 3893 | |
| 3896 | // calculate index into ptr | 3894 | // calculate index into ptr |
| 3897 | try self.emitWValue(index); | 3895 | try func.emitWValue(index); |
| 3898 | try self.addImm32(@bitCast(i32, @intCast(u32, elem_size))); | 3896 | try func.addImm32(@bitCast(i32, @intCast(u32, elem_size))); |
| 3899 | try self.addTag(.i32_mul); | 3897 | try func.addTag(.i32_mul); |
| 3900 | try self.addTag(.i32_add); | 3898 | try func.addTag(.i32_add); |
| 3901 | | 3899 | |
| 3902 | const result = try self.allocLocal(Type.i32); | 3900 | const result = try func.allocLocal(Type.i32); |
| 3903 | try self.addLabel(.local_set, result.local.value); | 3901 | try func.addLabel(.local_set, result.local.value); |
| 3904 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 3902 | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 3905 | } | 3903 | } |
| 3906 | | 3904 | |
| 3907 | fn airPtrBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { | 3905 | fn airPtrBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 3908 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 3906 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 3909 | const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data; | 3907 | const bin_op = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 3910 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 3908 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 3911 | | 3909 | |
| 3912 | const ptr = try self.resolveInst(bin_op.lhs); | 3910 | const ptr = try func.resolveInst(bin_op.lhs); |
| 3913 | const offset = try self.resolveInst(bin_op.rhs); | 3911 | const offset = try func.resolveInst(bin_op.rhs); |
| 3914 | const ptr_ty = self.air.typeOf(bin_op.lhs); | 3912 | const ptr_ty = func.air.typeOf(bin_op.lhs); |
| 3915 | const pointee_ty = switch (ptr_ty.ptrSize()) { | 3913 | const pointee_ty = switch (ptr_ty.ptrSize()) { |
| 3916 | .One => ptr_ty.childType().childType(), // ptr to array, so get array element type | 3914 | .One => ptr_ty.childType().childType(), // ptr to array, so get array element type |
| 3917 | else => ptr_ty.childType(), | 3915 | else => ptr_ty.childType(), |
| 3918 | }; | 3916 | }; |
| 3919 | | 3917 | |
| 3920 | const valtype = typeToValtype(Type.usize, self.target); | 3918 | const valtype = typeToValtype(Type.usize, func.target); |
| 3921 | const mul_opcode = buildOpcode(.{ .valtype1 = valtype, .op = .mul }); | 3919 | const mul_opcode = buildOpcode(.{ .valtype1 = valtype, .op = .mul }); |
| 3922 | const bin_opcode = buildOpcode(.{ .valtype1 = valtype, .op = op }); | 3920 | const bin_opcode = buildOpcode(.{ .valtype1 = valtype, .op = op }); |
| 3923 | | 3921 | |
| 3924 | try self.lowerToStack(ptr); | 3922 | try func.lowerToStack(ptr); |
| 3925 | try self.emitWValue(offset); | 3923 | try func.emitWValue(offset); |
| 3926 | try self.addImm32(@bitCast(i32, @intCast(u32, pointee_ty.abiSize(self.target)))); | 3924 | try func.addImm32(@bitCast(i32, @intCast(u32, pointee_ty.abiSize(func.target)))); |
| 3927 | try self.addTag(Mir.Inst.Tag.fromOpcode(mul_opcode)); | 3925 | try func.addTag(Mir.Inst.Tag.fromOpcode(mul_opcode)); |
| 3928 | try self.addTag(Mir.Inst.Tag.fromOpcode(bin_opcode)); | 3926 | try func.addTag(Mir.Inst.Tag.fromOpcode(bin_opcode)); |
| 3929 | | 3927 | |
| 3930 | const result = try self.allocLocal(Type.usize); | 3928 | const result = try func.allocLocal(Type.usize); |
| 3931 | try self.addLabel(.local_set, result.local.value); | 3929 | try func.addLabel(.local_set, result.local.value); |
| 3932 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 3930 | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 3933 | } | 3931 | } |
| 3934 | | 3932 | |
| 3935 | fn airMemset(self: *Self, inst: Air.Inst.Index) InnerError!void { | 3933 | fn airMemset(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 3936 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 3934 | const pl_op = func.air.instructions.items(.data)[inst].pl_op; |
| 3937 | const bin_op = self.air.extraData(Air.Bin, pl_op.payload).data; | 3935 | const bin_op = func.air.extraData(Air.Bin, pl_op.payload).data; |
| 3938 | | 3936 | |
| 3939 | const ptr = try self.resolveInst(pl_op.operand); | 3937 | const ptr = try func.resolveInst(pl_op.operand); |
| 3940 | const value = try self.resolveInst(bin_op.lhs); | 3938 | const value = try func.resolveInst(bin_op.lhs); |
| 3941 | const len = try self.resolveInst(bin_op.rhs); | 3939 | const len = try func.resolveInst(bin_op.rhs); |
| 3942 | try self.memset(ptr, len, value); | 3940 | try func.memset(ptr, len, value); |
| 3943 | | 3941 | |
| 3944 | self.finishAir(inst, .none, &.{pl_op.operand}); | 3942 | func.finishAir(inst, .none, &.{pl_op.operand}); |
| 3945 | } | 3943 | } |
| 3946 | | 3944 | |
| 3947 | /// Sets a region of memory at `ptr` to the value of `value` | 3945 | /// Sets a region of memory at `ptr` to the value of `value` |
| 3948 | /// When the user has enabled the bulk_memory feature, we lower | 3946 | /// When the user has enabled the bulk_memory feature, we lower |
| 3949 | /// this to wasm's memset instruction. When the feature is not present, | 3947 | /// this to wasm's memset instruction. When the feature is not present, |
| 3950 | /// we implement it manually. | 3948 | /// we implement it manually. |
| 3951 | fn memset(self: *Self, ptr: WValue, len: WValue, value: WValue) InnerError!void { | 3949 | fn memset(func: *CodeGen, ptr: WValue, len: WValue, value: WValue) InnerError!void { |
| 3952 | // When bulk_memory is enabled, we lower it to wasm's memset instruction. | 3950 | // When bulk_memory is enabled, we lower it to wasm's memset instruction. |
| 3953 | // If not, we lower it ourselves | 3951 | // If not, we lower it ourselves |
| 3954 | if (std.Target.wasm.featureSetHas(self.target.cpu.features, .bulk_memory)) { | 3952 | if (std.Target.wasm.featureSetHas(func.target.cpu.features, .bulk_memory)) { |
| 3955 | try self.lowerToStack(ptr); | 3953 | try func.lowerToStack(ptr); |
| 3956 | try self.emitWValue(value); | 3954 | try func.emitWValue(value); |
| 3957 | try self.emitWValue(len); | 3955 | try func.emitWValue(len); |
| 3958 | try self.addExtended(.memory_fill); | 3956 | try func.addExtended(.memory_fill); |
| 3959 | return; | 3957 | return; |
| 3960 | } | 3958 | } |
| 3961 | | 3959 | |
| ... | @@ -3972,14 +3970,14 @@ fn memset(self: *Self, ptr: WValue, len: WValue, value: WValue) InnerError!void | ... | @@ -3972,14 +3970,14 @@ fn memset(self: *Self, ptr: WValue, len: WValue, value: WValue) InnerError!void |
| 3972 | var offset: u32 = 0; | 3970 | var offset: u32 = 0; |
| 3973 | const base = ptr.offset(); | 3971 | const base = ptr.offset(); |
| 3974 | while (offset < length) : (offset += 1) { | 3972 | while (offset < length) : (offset += 1) { |
| 3975 | try self.emitWValue(ptr); | 3973 | try func.emitWValue(ptr); |
| 3976 | try self.emitWValue(value); | 3974 | try func.emitWValue(value); |
| 3977 | switch (self.arch()) { | 3975 | switch (func.arch()) { |
| 3978 | .wasm32 => { | 3976 | .wasm32 => { |
| 3979 | try self.addMemArg(.i32_store8, .{ .offset = base + offset, .alignment = 1 }); | 3977 | try func.addMemArg(.i32_store8, .{ .offset = base + offset, .alignment = 1 }); |
| 3980 | }, | 3978 | }, |
| 3981 | .wasm64 => { | 3979 | .wasm64 => { |
| 3982 | try self.addMemArg(.i64_store8, .{ .offset = base + offset, .alignment = 1 }); | 3980 | try func.addMemArg(.i64_store8, .{ .offset = base + offset, .alignment = 1 }); |
| 3983 | }, | 3981 | }, |
| 3984 | else => unreachable, | 3982 | else => unreachable, |
| 3985 | } | 3983 | } |
| ... | @@ -3988,378 +3986,378 @@ fn memset(self: *Self, ptr: WValue, len: WValue, value: WValue) InnerError!void | ... | @@ -3988,378 +3986,378 @@ fn memset(self: *Self, ptr: WValue, len: WValue, value: WValue) InnerError!void |
| 3988 | else => { | 3986 | else => { |
| 3989 | // TODO: We should probably lower this to a call to compiler_rt | 3987 | // TODO: We should probably lower this to a call to compiler_rt |
| 3990 | // But for now, we implement it manually | 3988 | // But for now, we implement it manually |
| 3991 | const offset = try self.ensureAllocLocal(Type.usize); // local for counter | 3989 | const offset = try func.ensureAllocLocal(Type.usize); // local for counter |
| 3992 | // outer block to jump to when loop is done | 3990 | // outer block to jump to when loop is done |
| 3993 | try self.startBlock(.block, wasm.block_empty); | 3991 | try func.startBlock(.block, wasm.block_empty); |
| 3994 | try self.startBlock(.loop, wasm.block_empty); | 3992 | try func.startBlock(.loop, wasm.block_empty); |
| 3995 | try self.emitWValue(offset); | 3993 | try func.emitWValue(offset); |
| 3996 | try self.emitWValue(len); | 3994 | try func.emitWValue(len); |
| 3997 | switch (self.arch()) { | 3995 | switch (func.arch()) { |
| 3998 | .wasm32 => try self.addTag(.i32_eq), | 3996 | .wasm32 => try func.addTag(.i32_eq), |
| 3999 | .wasm64 => try self.addTag(.i64_eq), | 3997 | .wasm64 => try func.addTag(.i64_eq), |
| 4000 | else => unreachable, | 3998 | else => unreachable, |
| 4001 | } | 3999 | } |
| 4002 | try self.addLabel(.br_if, 1); // jump out of loop into outer block (finished) | 4000 | try func.addLabel(.br_if, 1); // jump out of loop into outer block (finished) |
| 4003 | try self.emitWValue(ptr); | 4001 | try func.emitWValue(ptr); |
| 4004 | try self.emitWValue(offset); | 4002 | try func.emitWValue(offset); |
| 4005 | switch (self.arch()) { | 4003 | switch (func.arch()) { |
| 4006 | .wasm32 => try self.addTag(.i32_add), | 4004 | .wasm32 => try func.addTag(.i32_add), |
| 4007 | .wasm64 => try self.addTag(.i64_add), | 4005 | .wasm64 => try func.addTag(.i64_add), |
| 4008 | else => unreachable, | 4006 | else => unreachable, |
| 4009 | } | 4007 | } |
| 4010 | try self.emitWValue(value); | 4008 | try func.emitWValue(value); |
| 4011 | const mem_store_op: Mir.Inst.Tag = switch (self.arch()) { | 4009 | const mem_store_op: Mir.Inst.Tag = switch (func.arch()) { |
| 4012 | .wasm32 => .i32_store8, | 4010 | .wasm32 => .i32_store8, |
| 4013 | .wasm64 => .i64_store8, | 4011 | .wasm64 => .i64_store8, |
| 4014 | else => unreachable, | 4012 | else => unreachable, |
| 4015 | }; | 4013 | }; |
| 4016 | try self.addMemArg(mem_store_op, .{ .offset = ptr.offset(), .alignment = 1 }); | 4014 | try func.addMemArg(mem_store_op, .{ .offset = ptr.offset(), .alignment = 1 }); |
| 4017 | try self.emitWValue(offset); | 4015 | try func.emitWValue(offset); |
| 4018 | try self.addImm32(1); | 4016 | try func.addImm32(1); |
| 4019 | switch (self.arch()) { | 4017 | switch (func.arch()) { |
| 4020 | .wasm32 => try self.addTag(.i32_add), | 4018 | .wasm32 => try func.addTag(.i32_add), |
| 4021 | .wasm64 => try self.addTag(.i64_add), | 4019 | .wasm64 => try func.addTag(.i64_add), |
| 4022 | else => unreachable, | 4020 | else => unreachable, |
| 4023 | } | 4021 | } |
| 4024 | try self.addLabel(.local_set, offset.local.value); | 4022 | try func.addLabel(.local_set, offset.local.value); |
| 4025 | try self.addLabel(.br, 0); // jump to start of loop | 4023 | try func.addLabel(.br, 0); // jump to start of loop |
| 4026 | try self.endBlock(); | 4024 | try func.endBlock(); |
| 4027 | try self.endBlock(); | 4025 | try func.endBlock(); |
| 4028 | }, | 4026 | }, |
| 4029 | } | 4027 | } |
| 4030 | } | 4028 | } |
| 4031 | | 4029 | |
| 4032 | fn airArrayElemVal(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4030 | fn airArrayElemVal(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4033 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 4031 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 4034 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 4032 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 4035 | | 4033 | |
| 4036 | const array_ty = self.air.typeOf(bin_op.lhs); | 4034 | const array_ty = func.air.typeOf(bin_op.lhs); |
| 4037 | const array = try self.resolveInst(bin_op.lhs); | 4035 | const array = try func.resolveInst(bin_op.lhs); |
| 4038 | const index = try self.resolveInst(bin_op.rhs); | 4036 | const index = try func.resolveInst(bin_op.rhs); |
| 4039 | const elem_ty = array_ty.childType(); | 4037 | const elem_ty = array_ty.childType(); |
| 4040 | const elem_size = elem_ty.abiSize(self.target); | 4038 | const elem_size = elem_ty.abiSize(func.target); |
| 4041 | | 4039 | |
| 4042 | try self.lowerToStack(array); | 4040 | try func.lowerToStack(array); |
| 4043 | try self.emitWValue(index); | 4041 | try func.emitWValue(index); |
| 4044 | try self.addImm32(@bitCast(i32, @intCast(u32, elem_size))); | 4042 | try func.addImm32(@bitCast(i32, @intCast(u32, elem_size))); |
| 4045 | try self.addTag(.i32_mul); | 4043 | try func.addTag(.i32_mul); |
| 4046 | try self.addTag(.i32_add); | 4044 | try func.addTag(.i32_add); |
| 4047 | | 4045 | |
| 4048 | const elem_result = val: { | 4046 | const elem_result = val: { |
| 4049 | var result = try self.allocLocal(Type.usize); | 4047 | var result = try func.allocLocal(Type.usize); |
| 4050 | try self.addLabel(.local_set, result.local.value); | 4048 | try func.addLabel(.local_set, result.local.value); |
| 4051 | | 4049 | |
| 4052 | if (isByRef(elem_ty, self.target)) { | 4050 | if (isByRef(elem_ty, func.target)) { |
| 4053 | break :val result; | 4051 | break :val result; |
| 4054 | } | 4052 | } |
| 4055 | defer result.free(self); // only free if no longer needed and not returned like above | 4053 | defer result.free(func); // only free if no longer needed and not returned like above |
| 4056 | | 4054 | |
| 4057 | const elem_val = try self.load(result, elem_ty, 0); | 4055 | const elem_val = try func.load(result, elem_ty, 0); |
| 4058 | break :val try elem_val.toLocal(self, elem_ty); | 4056 | break :val try elem_val.toLocal(func, elem_ty); |
| 4059 | }; | 4057 | }; |
| 4060 | | 4058 | |
| 4061 | self.finishAir(inst, elem_result, &.{ bin_op.lhs, bin_op.rhs }); | 4059 | func.finishAir(inst, elem_result, &.{ bin_op.lhs, bin_op.rhs }); |
| 4062 | } | 4060 | } |
| 4063 | | 4061 | |
| 4064 | fn airFloatToInt(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4062 | fn airFloatToInt(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4065 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 4063 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 4066 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 4064 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 4067 | | 4065 | |
| 4068 | const operand = try self.resolveInst(ty_op.operand); | 4066 | const operand = try func.resolveInst(ty_op.operand); |
| 4069 | const dest_ty = self.air.typeOfIndex(inst); | 4067 | const dest_ty = func.air.typeOfIndex(inst); |
| 4070 | const op_ty = self.air.typeOf(ty_op.operand); | 4068 | const op_ty = func.air.typeOf(ty_op.operand); |
| 4071 | | 4069 | |
| 4072 | if (op_ty.abiSize(self.target) > 8) { | 4070 | if (op_ty.abiSize(func.target) > 8) { |
| 4073 | return self.fail("TODO: floatToInt for integers/floats with bitsize larger than 64 bits", .{}); | 4071 | return func.fail("TODO: floatToInt for integers/floats with bitsize larger than 64 bits", .{}); |
| 4074 | } | 4072 | } |
| 4075 | | 4073 | |
| 4076 | try self.emitWValue(operand); | 4074 | try func.emitWValue(operand); |
| 4077 | const op = buildOpcode(.{ | 4075 | const op = buildOpcode(.{ |
| 4078 | .op = .trunc, | 4076 | .op = .trunc, |
| 4079 | .valtype1 = typeToValtype(dest_ty, self.target), | 4077 | .valtype1 = typeToValtype(dest_ty, func.target), |
| 4080 | .valtype2 = typeToValtype(op_ty, self.target), | 4078 | .valtype2 = typeToValtype(op_ty, func.target), |
| 4081 | .signedness = if (dest_ty.isSignedInt()) .signed else .unsigned, | 4079 | .signedness = if (dest_ty.isSignedInt()) .signed else .unsigned, |
| 4082 | }); | 4080 | }); |
| 4083 | try self.addTag(Mir.Inst.Tag.fromOpcode(op)); | 4081 | try func.addTag(Mir.Inst.Tag.fromOpcode(op)); |
| 4084 | const wrapped = try self.wrapOperand(.{ .stack = {} }, dest_ty); | 4082 | const wrapped = try func.wrapOperand(.{ .stack = {} }, dest_ty); |
| 4085 | const result = try wrapped.toLocal(self, dest_ty); | 4083 | const result = try wrapped.toLocal(func, dest_ty); |
| 4086 | self.finishAir(inst, result, &.{ty_op.operand}); | 4084 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4087 | } | 4085 | } |
| 4088 | | 4086 | |
| 4089 | fn airIntToFloat(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4087 | fn airIntToFloat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4090 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 4088 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 4091 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 4089 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 4092 | | 4090 | |
| 4093 | const operand = try self.resolveInst(ty_op.operand); | 4091 | const operand = try func.resolveInst(ty_op.operand); |
| 4094 | const dest_ty = self.air.typeOfIndex(inst); | 4092 | const dest_ty = func.air.typeOfIndex(inst); |
| 4095 | const op_ty = self.air.typeOf(ty_op.operand); | 4093 | const op_ty = func.air.typeOf(ty_op.operand); |
| 4096 | | 4094 | |
| 4097 | if (op_ty.abiSize(self.target) > 8) { | 4095 | if (op_ty.abiSize(func.target) > 8) { |
| 4098 | return self.fail("TODO: intToFloat for integers/floats with bitsize larger than 64 bits", .{}); | 4096 | return func.fail("TODO: intToFloat for integers/floats with bitsize larger than 64 bits", .{}); |
| 4099 | } | 4097 | } |
| 4100 | | 4098 | |
| 4101 | try self.emitWValue(operand); | 4099 | try func.emitWValue(operand); |
| 4102 | const op = buildOpcode(.{ | 4100 | const op = buildOpcode(.{ |
| 4103 | .op = .convert, | 4101 | .op = .convert, |
| 4104 | .valtype1 = typeToValtype(dest_ty, self.target), | 4102 | .valtype1 = typeToValtype(dest_ty, func.target), |
| 4105 | .valtype2 = typeToValtype(op_ty, self.target), | 4103 | .valtype2 = typeToValtype(op_ty, func.target), |
| 4106 | .signedness = if (op_ty.isSignedInt()) .signed else .unsigned, | 4104 | .signedness = if (op_ty.isSignedInt()) .signed else .unsigned, |
| 4107 | }); | 4105 | }); |
| 4108 | try self.addTag(Mir.Inst.Tag.fromOpcode(op)); | 4106 | try func.addTag(Mir.Inst.Tag.fromOpcode(op)); |
| 4109 | | 4107 | |
| 4110 | const result = try self.allocLocal(dest_ty); | 4108 | const result = try func.allocLocal(dest_ty); |
| 4111 | try self.addLabel(.local_set, result.local.value); | 4109 | try func.addLabel(.local_set, result.local.value); |
| 4112 | self.finishAir(inst, result, &.{ty_op.operand}); | 4110 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4113 | } | 4111 | } |
| 4114 | | 4112 | |
| 4115 | fn airSplat(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4113 | fn airSplat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4116 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 4114 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 4117 | const operand = try self.resolveInst(ty_op.operand); | 4115 | const operand = try func.resolveInst(ty_op.operand); |
| 4118 | | 4116 | |
| 4119 | _ = operand; | 4117 | _ = operand; |
| 4120 | return self.fail("TODO: Implement wasm airSplat", .{}); | 4118 | return func.fail("TODO: Implement wasm airSplat", .{}); |
| 4121 | } | 4119 | } |
| 4122 | | 4120 | |
| 4123 | fn airSelect(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4121 | fn airSelect(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4124 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 4122 | const pl_op = func.air.instructions.items(.data)[inst].pl_op; |
| 4125 | const operand = try self.resolveInst(pl_op.operand); | 4123 | const operand = try func.resolveInst(pl_op.operand); |
| 4126 | | 4124 | |
| 4127 | _ = operand; | 4125 | _ = operand; |
| 4128 | return self.fail("TODO: Implement wasm airSelect", .{}); | 4126 | return func.fail("TODO: Implement wasm airSelect", .{}); |
| 4129 | } | 4127 | } |
| 4130 | | 4128 | |
| 4131 | fn airShuffle(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4129 | fn airShuffle(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4132 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 4130 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 4133 | const operand = try self.resolveInst(ty_op.operand); | 4131 | const operand = try func.resolveInst(ty_op.operand); |
| 4134 | | 4132 | |
| 4135 | _ = operand; | 4133 | _ = operand; |
| 4136 | return self.fail("TODO: Implement wasm airShuffle", .{}); | 4134 | return func.fail("TODO: Implement wasm airShuffle", .{}); |
| 4137 | } | 4135 | } |
| 4138 | | 4136 | |
| 4139 | fn airReduce(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4137 | fn airReduce(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4140 | const reduce = self.air.instructions.items(.data)[inst].reduce; | 4138 | const reduce = func.air.instructions.items(.data)[inst].reduce; |
| 4141 | const operand = try self.resolveInst(reduce.operand); | 4139 | const operand = try func.resolveInst(reduce.operand); |
| 4142 | | 4140 | |
| 4143 | _ = operand; | 4141 | _ = operand; |
| 4144 | return self.fail("TODO: Implement wasm airReduce", .{}); | 4142 | return func.fail("TODO: Implement wasm airReduce", .{}); |
| 4145 | } | 4143 | } |
| 4146 | | 4144 | |
| 4147 | fn airAggregateInit(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4145 | fn airAggregateInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4148 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 4146 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 4149 | const result_ty = self.air.typeOfIndex(inst); | 4147 | const result_ty = func.air.typeOfIndex(inst); |
| 4150 | const len = @intCast(usize, result_ty.arrayLen()); | 4148 | const len = @intCast(usize, result_ty.arrayLen()); |
| 4151 | const elements = @ptrCast([]const Air.Inst.Ref, self.air.extra[ty_pl.payload..][0..len]); | 4149 | const elements = @ptrCast([]const Air.Inst.Ref, func.air.extra[ty_pl.payload..][0..len]); |
| 4152 | | 4150 | |
| 4153 | const result: WValue = result_value: { | 4151 | const result: WValue = result_value: { |
| 4154 | if (self.liveness.isUnused(inst)) break :result_value WValue.none; | 4152 | if (func.liveness.isUnused(inst)) break :result_value WValue.none; |
| 4155 | switch (result_ty.zigTypeTag()) { | 4153 | switch (result_ty.zigTypeTag()) { |
| 4156 | .Array => { | 4154 | .Array => { |
| 4157 | const result = try self.allocStack(result_ty); | 4155 | const result = try func.allocStack(result_ty); |
| 4158 | const elem_ty = result_ty.childType(); | 4156 | const elem_ty = result_ty.childType(); |
| 4159 | const elem_size = @intCast(u32, elem_ty.abiSize(self.target)); | 4157 | const elem_size = @intCast(u32, elem_ty.abiSize(func.target)); |
| 4160 | | 4158 | |
| 4161 | // When the element type is by reference, we must copy the entire | 4159 | // When the element type is by reference, we must copy the entire |
| 4162 | // value. It is therefore safer to move the offset pointer and store | 4160 | // value. It is therefore safer to move the offset pointer and store |
| 4163 | // each value individually, instead of using store offsets. | 4161 | // each value individually, instead of using store offsets. |
| 4164 | if (isByRef(elem_ty, self.target)) { | 4162 | if (isByRef(elem_ty, func.target)) { |
| 4165 | // copy stack pointer into a temporary local, which is | 4163 | // copy stack pointer into a temporary local, which is |
| 4166 | // moved for each element to store each value in the right position. | 4164 | // moved for each element to store each value in the right position. |
| 4167 | const offset = try self.buildPointerOffset(result, 0, .new); | 4165 | const offset = try func.buildPointerOffset(result, 0, .new); |
| 4168 | for (elements) |elem, elem_index| { | 4166 | for (elements) |elem, elem_index| { |
| 4169 | const elem_val = try self.resolveInst(elem); | 4167 | const elem_val = try func.resolveInst(elem); |
| 4170 | try self.store(offset, elem_val, elem_ty, 0); | 4168 | try func.store(offset, elem_val, elem_ty, 0); |
| 4171 | | 4169 | |
| 4172 | if (elem_index < elements.len - 1) { | 4170 | if (elem_index < elements.len - 1) { |
| 4173 | _ = try self.buildPointerOffset(offset, elem_size, .modify); | 4171 | _ = try func.buildPointerOffset(offset, elem_size, .modify); |
| 4174 | } | 4172 | } |
| 4175 | } | 4173 | } |
| 4176 | } else { | 4174 | } else { |
| 4177 | var offset: u32 = 0; | 4175 | var offset: u32 = 0; |
| 4178 | for (elements) |elem| { | 4176 | for (elements) |elem| { |
| 4179 | const elem_val = try self.resolveInst(elem); | 4177 | const elem_val = try func.resolveInst(elem); |
| 4180 | try self.store(result, elem_val, elem_ty, offset); | 4178 | try func.store(result, elem_val, elem_ty, offset); |
| 4181 | offset += elem_size; | 4179 | offset += elem_size; |
| 4182 | } | 4180 | } |
| 4183 | } | 4181 | } |
| 4184 | break :result_value result; | 4182 | break :result_value result; |
| 4185 | }, | 4183 | }, |
| 4186 | .Struct => { | 4184 | .Struct => { |
| 4187 | const result = try self.allocStack(result_ty); | 4185 | const result = try func.allocStack(result_ty); |
| 4188 | const offset = try self.buildPointerOffset(result, 0, .new); // pointer to offset | 4186 | const offset = try func.buildPointerOffset(result, 0, .new); // pointer to offset |
| 4189 | for (elements) |elem, elem_index| { | 4187 | for (elements) |elem, elem_index| { |
| 4190 | if (result_ty.structFieldValueComptime(elem_index) != null) continue; | 4188 | if (result_ty.structFieldValueComptime(elem_index) != null) continue; |
| 4191 | | 4189 | |
| 4192 | const elem_ty = result_ty.structFieldType(elem_index); | 4190 | const elem_ty = result_ty.structFieldType(elem_index); |
| 4193 | const elem_size = @intCast(u32, elem_ty.abiSize(self.target)); | 4191 | const elem_size = @intCast(u32, elem_ty.abiSize(func.target)); |
| 4194 | const value = try self.resolveInst(elem); | 4192 | const value = try func.resolveInst(elem); |
| 4195 | try self.store(offset, value, elem_ty, 0); | 4193 | try func.store(offset, value, elem_ty, 0); |
| 4196 | | 4194 | |
| 4197 | if (elem_index < elements.len - 1) { | 4195 | if (elem_index < elements.len - 1) { |
| 4198 | _ = try self.buildPointerOffset(offset, elem_size, .modify); | 4196 | _ = try func.buildPointerOffset(offset, elem_size, .modify); |
| 4199 | } | 4197 | } |
| 4200 | } | 4198 | } |
| 4201 | | 4199 | |
| 4202 | break :result_value result; | 4200 | break :result_value result; |
| 4203 | }, | 4201 | }, |
| 4204 | .Vector => return self.fail("TODO: Wasm backend: implement airAggregateInit for vectors", .{}), | 4202 | .Vector => return func.fail("TODO: Wasm backend: implement airAggregateInit for vectors", .{}), |
| 4205 | else => unreachable, | 4203 | else => unreachable, |
| 4206 | } | 4204 | } |
| 4207 | }; | 4205 | }; |
| 4208 | self.finishAir(inst, result, &.{}); | 4206 | func.finishAir(inst, result, &.{}); |
| 4209 | } | 4207 | } |
| 4210 | | 4208 | |
| 4211 | fn airUnionInit(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4209 | fn airUnionInit(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4212 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 4210 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 4213 | const extra = self.air.extraData(Air.UnionInit, ty_pl.payload).data; | 4211 | const extra = func.air.extraData(Air.UnionInit, ty_pl.payload).data; |
| 4214 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{extra.init}); | 4212 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{extra.init}); |
| 4215 | | 4213 | |
| 4216 | const result = result: { | 4214 | const result = result: { |
| 4217 | const union_ty = self.air.typeOfIndex(inst); | 4215 | const union_ty = func.air.typeOfIndex(inst); |
| 4218 | const layout = union_ty.unionGetLayout(self.target); | 4216 | const layout = union_ty.unionGetLayout(func.target); |
| 4219 | if (layout.payload_size == 0) { | 4217 | if (layout.payload_size == 0) { |
| 4220 | if (layout.tag_size == 0) { | 4218 | if (layout.tag_size == 0) { |
| 4221 | break :result WValue{ .none = {} }; | 4219 | break :result WValue{ .none = {} }; |
| 4222 | } | 4220 | } |
| 4223 | assert(!isByRef(union_ty, self.target)); | 4221 | assert(!isByRef(union_ty, func.target)); |
| 4224 | break :result WValue{ .imm32 = extra.field_index }; | 4222 | break :result WValue{ .imm32 = extra.field_index }; |
| 4225 | } | 4223 | } |
| 4226 | assert(isByRef(union_ty, self.target)); | 4224 | assert(isByRef(union_ty, func.target)); |
| 4227 | | 4225 | |
| 4228 | const result_ptr = try self.allocStack(union_ty); | 4226 | const result_ptr = try func.allocStack(union_ty); |
| 4229 | const payload = try self.resolveInst(extra.init); | 4227 | const payload = try func.resolveInst(extra.init); |
| 4230 | const union_obj = union_ty.cast(Type.Payload.Union).?.data; | 4228 | const union_obj = union_ty.cast(Type.Payload.Union).?.data; |
| 4231 | assert(union_obj.haveFieldTypes()); | 4229 | assert(union_obj.haveFieldTypes()); |
| 4232 | const field = union_obj.fields.values()[extra.field_index]; | 4230 | const field = union_obj.fields.values()[extra.field_index]; |
| 4233 | | 4231 | |
| 4234 | if (layout.tag_align >= layout.payload_align) { | 4232 | if (layout.tag_align >= layout.payload_align) { |
| 4235 | const payload_ptr = try self.buildPointerOffset(result_ptr, layout.tag_size, .new); | 4233 | const payload_ptr = try func.buildPointerOffset(result_ptr, layout.tag_size, .new); |
| 4236 | try self.store(payload_ptr, payload, field.ty, 0); | 4234 | try func.store(payload_ptr, payload, field.ty, 0); |
| 4237 | } else { | 4235 | } else { |
| 4238 | try self.store(result_ptr, payload, field.ty, 0); | 4236 | try func.store(result_ptr, payload, field.ty, 0); |
| 4239 | } | 4237 | } |
| 4240 | break :result result_ptr; | 4238 | break :result result_ptr; |
| 4241 | }; | 4239 | }; |
| 4242 | | 4240 | |
| 4243 | self.finishAir(inst, result, &.{extra.init}); | 4241 | func.finishAir(inst, result, &.{extra.init}); |
| 4244 | } | 4242 | } |
| 4245 | | 4243 | |
| 4246 | fn airPrefetch(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4244 | fn airPrefetch(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4247 | const prefetch = self.air.instructions.items(.data)[inst].prefetch; | 4245 | const prefetch = func.air.instructions.items(.data)[inst].prefetch; |
| 4248 | self.finishAir(inst, .none, &.{prefetch.ptr}); | 4246 | func.finishAir(inst, .none, &.{prefetch.ptr}); |
| 4249 | } | 4247 | } |
| 4250 | | 4248 | |
| 4251 | fn airWasmMemorySize(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4249 | fn airWasmMemorySize(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4252 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 4250 | const pl_op = func.air.instructions.items(.data)[inst].pl_op; |
| 4253 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{pl_op.operand}); | 4251 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{pl_op.operand}); |
| 4254 | | 4252 | |
| 4255 | const result = try self.allocLocal(self.air.typeOfIndex(inst)); | 4253 | const result = try func.allocLocal(func.air.typeOfIndex(inst)); |
| 4256 | try self.addLabel(.memory_size, pl_op.payload); | 4254 | try func.addLabel(.memory_size, pl_op.payload); |
| 4257 | try self.addLabel(.local_set, result.local.value); | 4255 | try func.addLabel(.local_set, result.local.value); |
| 4258 | self.finishAir(inst, result, &.{pl_op.operand}); | 4256 | func.finishAir(inst, result, &.{pl_op.operand}); |
| 4259 | } | 4257 | } |
| 4260 | | 4258 | |
| 4261 | fn airWasmMemoryGrow(self: *Self, inst: Air.Inst.Index) !void { | 4259 | fn airWasmMemoryGrow(func: *CodeGen, inst: Air.Inst.Index) !void { |
| 4262 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 4260 | const pl_op = func.air.instructions.items(.data)[inst].pl_op; |
| 4263 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{pl_op.operand}); | 4261 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{pl_op.operand}); |
| 4264 | | 4262 | |
| 4265 | const operand = try self.resolveInst(pl_op.operand); | 4263 | const operand = try func.resolveInst(pl_op.operand); |
| 4266 | const result = try self.allocLocal(self.air.typeOfIndex(inst)); | 4264 | const result = try func.allocLocal(func.air.typeOfIndex(inst)); |
| 4267 | try self.emitWValue(operand); | 4265 | try func.emitWValue(operand); |
| 4268 | try self.addLabel(.memory_grow, pl_op.payload); | 4266 | try func.addLabel(.memory_grow, pl_op.payload); |
| 4269 | try self.addLabel(.local_set, result.local.value); | 4267 | try func.addLabel(.local_set, result.local.value); |
| 4270 | self.finishAir(inst, result, &.{pl_op.operand}); | 4268 | func.finishAir(inst, result, &.{pl_op.operand}); |
| 4271 | } | 4269 | } |
| 4272 | | 4270 | |
| 4273 | fn cmpOptionals(self: *Self, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue { | 4271 | fn cmpOptionals(func: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue { |
| 4274 | assert(operand_ty.hasRuntimeBitsIgnoreComptime()); | 4272 | assert(operand_ty.hasRuntimeBitsIgnoreComptime()); |
| 4275 | assert(op == .eq or op == .neq); | 4273 | assert(op == .eq or op == .neq); |
| 4276 | var buf: Type.Payload.ElemType = undefined; | 4274 | var buf: Type.Payload.ElemType = undefined; |
| 4277 | const payload_ty = operand_ty.optionalChild(&buf); | 4275 | const payload_ty = operand_ty.optionalChild(&buf); |
| 4278 | const offset = @intCast(u32, operand_ty.abiSize(self.target) - payload_ty.abiSize(self.target)); | 4276 | const offset = @intCast(u32, operand_ty.abiSize(func.target) - payload_ty.abiSize(func.target)); |
| 4279 | | 4277 | |
| 4280 | // We store the final result in here that will be validated | 4278 | // We store the final result in here that will be validated |
| 4281 | // if the optional is truly equal. | 4279 | // if the optional is truly equal. |
| 4282 | var result = try self.ensureAllocLocal(Type.initTag(.i32)); | 4280 | var result = try func.ensureAllocLocal(Type.initTag(.i32)); |
| 4283 | defer result.free(self); | 4281 | defer result.free(func); |
| 4284 | | 4282 | |
| 4285 | try self.startBlock(.block, wasm.block_empty); | 4283 | try func.startBlock(.block, wasm.block_empty); |
| 4286 | _ = try self.isNull(lhs, operand_ty, .i32_eq); | 4284 | _ = try func.isNull(lhs, operand_ty, .i32_eq); |
| 4287 | _ = try self.isNull(rhs, operand_ty, .i32_eq); | 4285 | _ = try func.isNull(rhs, operand_ty, .i32_eq); |
| 4288 | try self.addTag(.i32_ne); // inverse so we can exit early | 4286 | try func.addTag(.i32_ne); // inverse so we can exit early |
| 4289 | try self.addLabel(.br_if, 0); | 4287 | try func.addLabel(.br_if, 0); |
| 4290 | | 4288 | |
| 4291 | _ = try self.load(lhs, payload_ty, offset); | 4289 | _ = try func.load(lhs, payload_ty, offset); |
| 4292 | _ = try self.load(rhs, payload_ty, offset); | 4290 | _ = try func.load(rhs, payload_ty, offset); |
| 4293 | const opcode = buildOpcode(.{ .op = .ne, .valtype1 = typeToValtype(payload_ty, self.target) }); | 4291 | const opcode = buildOpcode(.{ .op = .ne, .valtype1 = typeToValtype(payload_ty, func.target) }); |
| 4294 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); | 4292 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 4295 | try self.addLabel(.br_if, 0); | 4293 | try func.addLabel(.br_if, 0); |
| 4296 | | 4294 | |
| 4297 | try self.addImm32(1); | 4295 | try func.addImm32(1); |
| 4298 | try self.addLabel(.local_set, result.local.value); | 4296 | try func.addLabel(.local_set, result.local.value); |
| 4299 | try self.endBlock(); | 4297 | try func.endBlock(); |
| 4300 | | 4298 | |
| 4301 | try self.emitWValue(result); | 4299 | try func.emitWValue(result); |
| 4302 | try self.addImm32(0); | 4300 | try func.addImm32(0); |
| 4303 | try self.addTag(if (op == .eq) .i32_ne else .i32_eq); | 4301 | try func.addTag(if (op == .eq) .i32_ne else .i32_eq); |
| 4304 | return WValue{ .stack = {} }; | 4302 | return WValue{ .stack = {} }; |
| 4305 | } | 4303 | } |
| 4306 | | 4304 | |
| 4307 | /// Compares big integers by checking both its high bits and low bits. | 4305 | /// Compares big integers by checking both its high bits and low bits. |
| 4308 | /// NOTE: Leaves the result of the comparison on top of the stack. | 4306 | /// NOTE: Leaves the result of the comparison on top of the stack. |
| 4309 | /// TODO: Lower this to compiler_rt call when bitsize > 128 | 4307 | /// TODO: Lower this to compiler_rt call when bitsize > 128 |
| 4310 | fn cmpBigInt(self: *Self, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue { | 4308 | fn cmpBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op: std.math.CompareOperator) InnerError!WValue { |
| 4311 | assert(operand_ty.abiSize(self.target) >= 16); | 4309 | assert(operand_ty.abiSize(func.target) >= 16); |
| 4312 | assert(!(lhs != .stack and rhs == .stack)); | 4310 | assert(!(lhs != .stack and rhs == .stack)); |
| 4313 | if (operand_ty.intInfo(self.target).bits > 128) { | 4311 | if (operand_ty.intInfo(func.target).bits > 128) { |
| 4314 | return self.fail("TODO: Support cmpBigInt for integer bitsize: '{d}'", .{operand_ty.intInfo(self.target).bits}); | 4312 | return func.fail("TODO: Support cmpBigInt for integer bitsize: '{d}'", .{operand_ty.intInfo(func.target).bits}); |
| 4315 | } | 4313 | } |
| 4316 | | 4314 | |
| 4317 | var lhs_high_bit = try (try self.load(lhs, Type.u64, 0)).toLocal(self, Type.u64); | 4315 | var lhs_high_bit = try (try func.load(lhs, Type.u64, 0)).toLocal(func, Type.u64); |
| 4318 | defer lhs_high_bit.free(self); | 4316 | defer lhs_high_bit.free(func); |
| 4319 | var rhs_high_bit = try (try self.load(rhs, Type.u64, 0)).toLocal(self, Type.u64); | 4317 | var rhs_high_bit = try (try func.load(rhs, Type.u64, 0)).toLocal(func, Type.u64); |
| 4320 | defer rhs_high_bit.free(self); | 4318 | defer rhs_high_bit.free(func); |
| 4321 | | 4319 | |
| 4322 | switch (op) { | 4320 | switch (op) { |
| 4323 | .eq, .neq => { | 4321 | .eq, .neq => { |
| 4324 | const xor_high = try self.binOp(lhs_high_bit, rhs_high_bit, Type.u64, .xor); | 4322 | const xor_high = try func.binOp(lhs_high_bit, rhs_high_bit, Type.u64, .xor); |
| 4325 | const lhs_low_bit = try self.load(lhs, Type.u64, 8); | 4323 | const lhs_low_bit = try func.load(lhs, Type.u64, 8); |
| 4326 | const rhs_low_bit = try self.load(rhs, Type.u64, 8); | 4324 | const rhs_low_bit = try func.load(rhs, Type.u64, 8); |
| 4327 | const xor_low = try self.binOp(lhs_low_bit, rhs_low_bit, Type.u64, .xor); | 4325 | const xor_low = try func.binOp(lhs_low_bit, rhs_low_bit, Type.u64, .xor); |
| 4328 | const or_result = try self.binOp(xor_high, xor_low, Type.u64, .@"or"); | 4326 | const or_result = try func.binOp(xor_high, xor_low, Type.u64, .@"or"); |
| 4329 | | 4327 | |
| 4330 | switch (op) { | 4328 | switch (op) { |
| 4331 | .eq => return self.cmp(or_result, .{ .imm64 = 0 }, Type.u64, .eq), | 4329 | .eq => return func.cmp(or_result, .{ .imm64 = 0 }, Type.u64, .eq), |
| 4332 | .neq => return self.cmp(or_result, .{ .imm64 = 0 }, Type.u64, .neq), | 4330 | .neq => return func.cmp(or_result, .{ .imm64 = 0 }, Type.u64, .neq), |
| 4333 | else => unreachable, | 4331 | else => unreachable, |
| 4334 | } | 4332 | } |
| 4335 | }, | 4333 | }, |
| 4336 | else => { | 4334 | else => { |
| 4337 | const ty = if (operand_ty.isSignedInt()) Type.i64 else Type.u64; | 4335 | const ty = if (operand_ty.isSignedInt()) Type.i64 else Type.u64; |
| 4338 | // leave those value on top of the stack for '.select' | 4336 | // leave those value on top of the stack for '.select' |
| 4339 | const lhs_low_bit = try self.load(lhs, Type.u64, 8); | 4337 | const lhs_low_bit = try func.load(lhs, Type.u64, 8); |
| 4340 | const rhs_low_bit = try self.load(rhs, Type.u64, 8); | 4338 | const rhs_low_bit = try func.load(rhs, Type.u64, 8); |
| 4341 | _ = try self.cmp(lhs_low_bit, rhs_low_bit, ty, op); | 4339 | _ = try func.cmp(lhs_low_bit, rhs_low_bit, ty, op); |
| 4342 | _ = try self.cmp(lhs_high_bit, rhs_high_bit, ty, op); | 4340 | _ = try func.cmp(lhs_high_bit, rhs_high_bit, ty, op); |
| 4343 | _ = try self.cmp(lhs_high_bit, rhs_high_bit, ty, .eq); | 4341 | _ = try func.cmp(lhs_high_bit, rhs_high_bit, ty, .eq); |
| 4344 | try self.addTag(.select); | 4342 | try func.addTag(.select); |
| 4345 | }, | 4343 | }, |
| 4346 | } | 4344 | } |
| 4347 | | 4345 | |
| 4348 | return WValue{ .stack = {} }; | 4346 | return WValue{ .stack = {} }; |
| 4349 | } | 4347 | } |
| 4350 | | 4348 | |
| 4351 | fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4349 | fn airSetUnionTag(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4352 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 4350 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 4353 | const un_ty = self.air.typeOf(bin_op.lhs).childType(); | 4351 | const un_ty = func.air.typeOf(bin_op.lhs).childType(); |
| 4354 | const tag_ty = self.air.typeOf(bin_op.rhs); | 4352 | const tag_ty = func.air.typeOf(bin_op.rhs); |
| 4355 | const layout = un_ty.unionGetLayout(self.target); | 4353 | const layout = un_ty.unionGetLayout(func.target); |
| 4356 | if (layout.tag_size == 0) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 4354 | if (layout.tag_size == 0) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 4357 | | 4355 | |
| 4358 | const union_ptr = try self.resolveInst(bin_op.lhs); | 4356 | const union_ptr = try func.resolveInst(bin_op.lhs); |
| 4359 | const new_tag = try self.resolveInst(bin_op.rhs); | 4357 | const new_tag = try func.resolveInst(bin_op.rhs); |
| 4360 | if (layout.payload_size == 0) { | 4358 | if (layout.payload_size == 0) { |
| 4361 | try self.store(union_ptr, new_tag, tag_ty, 0); | 4359 | try func.store(union_ptr, new_tag, tag_ty, 0); |
| 4362 | return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 4360 | return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 4363 | } | 4361 | } |
| 4364 | | 4362 | |
| 4365 | // when the tag alignment is smaller than the payload, the field will be stored | 4363 | // when the tag alignment is smaller than the payload, the field will be stored |
| ... | @@ -4367,54 +4365,54 @@ fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -4367,54 +4365,54 @@ fn airSetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4367 | const offset = if (layout.tag_align < layout.payload_align) blk: { | 4365 | const offset = if (layout.tag_align < layout.payload_align) blk: { |
| 4368 | break :blk @intCast(u32, layout.payload_size); | 4366 | break :blk @intCast(u32, layout.payload_size); |
| 4369 | } else @as(u32, 0); | 4367 | } else @as(u32, 0); |
| 4370 | try self.store(union_ptr, new_tag, tag_ty, offset); | 4368 | try func.store(union_ptr, new_tag, tag_ty, offset); |
| 4371 | self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 4369 | func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 4372 | } | 4370 | } |
| 4373 | | 4371 | |
| 4374 | fn airGetUnionTag(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4372 | fn airGetUnionTag(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4375 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 4373 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 4376 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 4374 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 4377 | | 4375 | |
| 4378 | const un_ty = self.air.typeOf(ty_op.operand); | 4376 | const un_ty = func.air.typeOf(ty_op.operand); |
| 4379 | const tag_ty = self.air.typeOfIndex(inst); | 4377 | const tag_ty = func.air.typeOfIndex(inst); |
| 4380 | const layout = un_ty.unionGetLayout(self.target); | 4378 | const layout = un_ty.unionGetLayout(func.target); |
| 4381 | if (layout.tag_size == 0) return self.finishAir(inst, .none, &.{ty_op.operand}); | 4379 | if (layout.tag_size == 0) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 4382 | | 4380 | |
| 4383 | const operand = try self.resolveInst(ty_op.operand); | 4381 | const operand = try func.resolveInst(ty_op.operand); |
| 4384 | // when the tag alignment is smaller than the payload, the field will be stored | 4382 | // when the tag alignment is smaller than the payload, the field will be stored |
| 4385 | // after the payload. | 4383 | // after the payload. |
| 4386 | const offset = if (layout.tag_align < layout.payload_align) blk: { | 4384 | const offset = if (layout.tag_align < layout.payload_align) blk: { |
| 4387 | break :blk @intCast(u32, layout.payload_size); | 4385 | break :blk @intCast(u32, layout.payload_size); |
| 4388 | } else @as(u32, 0); | 4386 | } else @as(u32, 0); |
| 4389 | const tag = try self.load(operand, tag_ty, offset); | 4387 | const tag = try func.load(operand, tag_ty, offset); |
| 4390 | const result = try tag.toLocal(self, tag_ty); | 4388 | const result = try tag.toLocal(func, tag_ty); |
| 4391 | self.finishAir(inst, result, &.{ty_op.operand}); | 4389 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4392 | } | 4390 | } |
| 4393 | | 4391 | |
| 4394 | fn airFpext(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4392 | fn airFpext(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4395 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 4393 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 4396 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 4394 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 4397 | | 4395 | |
| 4398 | const dest_ty = self.air.typeOfIndex(inst); | 4396 | const dest_ty = func.air.typeOfIndex(inst); |
| 4399 | const operand = try self.resolveInst(ty_op.operand); | 4397 | const operand = try func.resolveInst(ty_op.operand); |
| 4400 | const extended = try self.fpext(operand, self.air.typeOf(ty_op.operand), dest_ty); | 4398 | const extended = try func.fpext(operand, func.air.typeOf(ty_op.operand), dest_ty); |
| 4401 | const result = try extended.toLocal(self, dest_ty); | 4399 | const result = try extended.toLocal(func, dest_ty); |
| 4402 | self.finishAir(inst, result, &.{ty_op.operand}); | 4400 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4403 | } | 4401 | } |
| 4404 | | 4402 | |
| 4405 | /// Extends a float from a given `Type` to a larger wanted `Type` | 4403 | /// Extends a float from a given `Type` to a larger wanted `Type` |
| 4406 | /// NOTE: Leaves the result on the stack | 4404 | /// NOTE: Leaves the result on the stack |
| 4407 | fn fpext(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!WValue { | 4405 | fn fpext(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerError!WValue { |
| 4408 | const given_bits = given.floatBits(self.target); | 4406 | const given_bits = given.floatBits(func.target); |
| 4409 | const wanted_bits = wanted.floatBits(self.target); | 4407 | const wanted_bits = wanted.floatBits(func.target); |
| 4410 | | 4408 | |
| 4411 | if (wanted_bits == 64 and given_bits == 32) { | 4409 | if (wanted_bits == 64 and given_bits == 32) { |
| 4412 | try self.emitWValue(operand); | 4410 | try func.emitWValue(operand); |
| 4413 | try self.addTag(.f64_promote_f32); | 4411 | try func.addTag(.f64_promote_f32); |
| 4414 | return WValue{ .stack = {} }; | 4412 | return WValue{ .stack = {} }; |
| 4415 | } else if (given_bits == 16) { | 4413 | } else if (given_bits == 16) { |
| 4416 | // call __extendhfsf2(f16) f32 | 4414 | // call __extendhfsf2(f16) f32 |
| 4417 | const f32_result = try self.callIntrinsic( | 4415 | const f32_result = try func.callIntrinsic( |
| 4418 | "__extendhfsf2", | 4416 | "__extendhfsf2", |
| 4419 | &.{Type.f16}, | 4417 | &.{Type.f16}, |
| 4420 | Type.f32, | 4418 | Type.f32, |
| ... | @@ -4425,162 +4423,162 @@ fn fpext(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!WVa | ... | @@ -4425,162 +4423,162 @@ fn fpext(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!WVa |
| 4425 | return f32_result; | 4423 | return f32_result; |
| 4426 | } | 4424 | } |
| 4427 | if (wanted_bits == 64) { | 4425 | if (wanted_bits == 64) { |
| 4428 | try self.addTag(.f64_promote_f32); | 4426 | try func.addTag(.f64_promote_f32); |
| 4429 | return WValue{ .stack = {} }; | 4427 | return WValue{ .stack = {} }; |
| 4430 | } | 4428 | } |
| 4431 | return self.fail("TODO: Implement 'fpext' for floats with bitsize: {d}", .{wanted_bits}); | 4429 | return func.fail("TODO: Implement 'fpext' for floats with bitsize: {d}", .{wanted_bits}); |
| 4432 | } else { | 4430 | } else { |
| 4433 | // TODO: Emit a call to compiler-rt to extend the float. e.g. __extendhfsf2 | 4431 | // TODO: Emit a call to compiler-rt to extend the float. e.g. __extendhfsf2 |
| 4434 | return self.fail("TODO: Implement 'fpext' for floats with bitsize: {d}", .{wanted_bits}); | 4432 | return func.fail("TODO: Implement 'fpext' for floats with bitsize: {d}", .{wanted_bits}); |
| 4435 | } | 4433 | } |
| 4436 | } | 4434 | } |
| 4437 | | 4435 | |
| 4438 | fn airFptrunc(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4436 | fn airFptrunc(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4439 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 4437 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 4440 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 4438 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 4441 | | 4439 | |
| 4442 | const dest_ty = self.air.typeOfIndex(inst); | 4440 | const dest_ty = func.air.typeOfIndex(inst); |
| 4443 | const operand = try self.resolveInst(ty_op.operand); | 4441 | const operand = try func.resolveInst(ty_op.operand); |
| 4444 | const trunc = try self.fptrunc(operand, self.air.typeOf(ty_op.operand), dest_ty); | 4442 | const trunc = try func.fptrunc(operand, func.air.typeOf(ty_op.operand), dest_ty); |
| 4445 | const result = try trunc.toLocal(self, dest_ty); | 4443 | const result = try trunc.toLocal(func, dest_ty); |
| 4446 | self.finishAir(inst, result, &.{ty_op.operand}); | 4444 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4447 | } | 4445 | } |
| 4448 | | 4446 | |
| 4449 | /// Truncates a float from a given `Type` to its wanted `Type` | 4447 | /// Truncates a float from a given `Type` to its wanted `Type` |
| 4450 | /// NOTE: The result value remains on the stack | 4448 | /// NOTE: The result value remains on the stack |
| 4451 | fn fptrunc(self: *Self, operand: WValue, given: Type, wanted: Type) InnerError!WValue { | 4449 | fn fptrunc(func: *CodeGen, operand: WValue, given: Type, wanted: Type) InnerError!WValue { |
| 4452 | const given_bits = given.floatBits(self.target); | 4450 | const given_bits = given.floatBits(func.target); |
| 4453 | const wanted_bits = wanted.floatBits(self.target); | 4451 | const wanted_bits = wanted.floatBits(func.target); |
| 4454 | | 4452 | |
| 4455 | if (wanted_bits == 32 and given_bits == 64) { | 4453 | if (wanted_bits == 32 and given_bits == 64) { |
| 4456 | try self.emitWValue(operand); | 4454 | try func.emitWValue(operand); |
| 4457 | try self.addTag(.f32_demote_f64); | 4455 | try func.addTag(.f32_demote_f64); |
| 4458 | return WValue{ .stack = {} }; | 4456 | return WValue{ .stack = {} }; |
| 4459 | } else if (wanted_bits == 16) { | 4457 | } else if (wanted_bits == 16) { |
| 4460 | const op: WValue = if (given_bits == 64) blk: { | 4458 | const op: WValue = if (given_bits == 64) blk: { |
| 4461 | try self.emitWValue(operand); | 4459 | try func.emitWValue(operand); |
| 4462 | try self.addTag(.f32_demote_f64); | 4460 | try func.addTag(.f32_demote_f64); |
| 4463 | break :blk WValue{ .stack = {} }; | 4461 | break :blk WValue{ .stack = {} }; |
| 4464 | } else operand; | 4462 | } else operand; |
| 4465 | | 4463 | |
| 4466 | // call __truncsfhf2(f32) f16 | 4464 | // call __truncsfhf2(f32) f16 |
| 4467 | return self.callIntrinsic("__truncsfhf2", &.{Type.f32}, Type.f16, &.{op}); | 4465 | return func.callIntrinsic("__truncsfhf2", &.{Type.f32}, Type.f16, &.{op}); |
| 4468 | } else { | 4466 | } else { |
| 4469 | // TODO: Emit a call to compiler-rt to trunc the float. e.g. __truncdfhf2 | 4467 | // TODO: Emit a call to compiler-rt to trunc the float. e.g. __truncdfhf2 |
| 4470 | return self.fail("TODO: Implement 'fptrunc' for floats with bitsize: {d}", .{wanted_bits}); | 4468 | return func.fail("TODO: Implement 'fptrunc' for floats with bitsize: {d}", .{wanted_bits}); |
| 4471 | } | 4469 | } |
| 4472 | } | 4470 | } |
| 4473 | | 4471 | |
| 4474 | fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4472 | fn airErrUnionPayloadPtrSet(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4475 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 4473 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 4476 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 4474 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 4477 | | 4475 | |
| 4478 | const err_set_ty = self.air.typeOf(ty_op.operand).childType(); | 4476 | const err_set_ty = func.air.typeOf(ty_op.operand).childType(); |
| 4479 | const payload_ty = err_set_ty.errorUnionPayload(); | 4477 | const payload_ty = err_set_ty.errorUnionPayload(); |
| 4480 | const operand = try self.resolveInst(ty_op.operand); | 4478 | const operand = try func.resolveInst(ty_op.operand); |
| 4481 | | 4479 | |
| 4482 | // set error-tag to '0' to annotate error union is non-error | 4480 | // set error-tag to '0' to annotate error union is non-error |
| 4483 | try self.store( | 4481 | try func.store( |
| 4484 | operand, | 4482 | operand, |
| 4485 | .{ .imm32 = 0 }, | 4483 | .{ .imm32 = 0 }, |
| 4486 | Type.anyerror, | 4484 | Type.anyerror, |
| 4487 | @intCast(u32, errUnionErrorOffset(payload_ty, self.target)), | 4485 | @intCast(u32, errUnionErrorOffset(payload_ty, func.target)), |
| 4488 | ); | 4486 | ); |
| 4489 | | 4487 | |
| 4490 | const result = result: { | 4488 | const result = result: { |
| 4491 | if (self.liveness.isUnused(inst)) break :result WValue{ .none = {} }; | 4489 | if (func.liveness.isUnused(inst)) break :result WValue{ .none = {} }; |
| 4492 | | 4490 | |
| 4493 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { | 4491 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 4494 | break :result self.reuseOperand(ty_op.operand, operand); | 4492 | break :result func.reuseOperand(ty_op.operand, operand); |
| 4495 | } | 4493 | } |
| 4496 | | 4494 | |
| 4497 | break :result try self.buildPointerOffset(operand, @intCast(u32, errUnionPayloadOffset(payload_ty, self.target)), .new); | 4495 | break :result try func.buildPointerOffset(operand, @intCast(u32, errUnionPayloadOffset(payload_ty, func.target)), .new); |
| 4498 | }; | 4496 | }; |
| 4499 | self.finishAir(inst, result, &.{ty_op.operand}); | 4497 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4500 | } | 4498 | } |
| 4501 | | 4499 | |
| 4502 | fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4500 | fn airFieldParentPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4503 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 4501 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 4504 | const extra = self.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; | 4502 | const extra = func.air.extraData(Air.FieldParentPtr, ty_pl.payload).data; |
| 4505 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{extra.field_ptr}); | 4503 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{extra.field_ptr}); |
| 4506 | | 4504 | |
| 4507 | const field_ptr = try self.resolveInst(extra.field_ptr); | 4505 | const field_ptr = try func.resolveInst(extra.field_ptr); |
| 4508 | const struct_ty = self.air.getRefType(ty_pl.ty).childType(); | 4506 | const struct_ty = func.air.getRefType(ty_pl.ty).childType(); |
| 4509 | const field_offset = struct_ty.structFieldOffset(extra.field_index, self.target); | 4507 | const field_offset = struct_ty.structFieldOffset(extra.field_index, func.target); |
| 4510 | | 4508 | |
| 4511 | const result = if (field_offset != 0) result: { | 4509 | const result = if (field_offset != 0) result: { |
| 4512 | const base = try self.buildPointerOffset(field_ptr, 0, .new); | 4510 | const base = try func.buildPointerOffset(field_ptr, 0, .new); |
| 4513 | try self.addLabel(.local_get, base.local.value); | 4511 | try func.addLabel(.local_get, base.local.value); |
| 4514 | try self.addImm32(@bitCast(i32, @intCast(u32, field_offset))); | 4512 | try func.addImm32(@bitCast(i32, @intCast(u32, field_offset))); |
| 4515 | try self.addTag(.i32_sub); | 4513 | try func.addTag(.i32_sub); |
| 4516 | try self.addLabel(.local_set, base.local.value); | 4514 | try func.addLabel(.local_set, base.local.value); |
| 4517 | break :result base; | 4515 | break :result base; |
| 4518 | } else self.reuseOperand(extra.field_ptr, field_ptr); | 4516 | } else func.reuseOperand(extra.field_ptr, field_ptr); |
| 4519 | | 4517 | |
| 4520 | self.finishAir(inst, result, &.{extra.field_ptr}); | 4518 | func.finishAir(inst, result, &.{extra.field_ptr}); |
| 4521 | } | 4519 | } |
| 4522 | | 4520 | |
| 4523 | fn airMemcpy(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4521 | fn airMemcpy(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4524 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 4522 | const pl_op = func.air.instructions.items(.data)[inst].pl_op; |
| 4525 | const bin_op = self.air.extraData(Air.Bin, pl_op.payload).data; | 4523 | const bin_op = func.air.extraData(Air.Bin, pl_op.payload).data; |
| 4526 | const dst = try self.resolveInst(pl_op.operand); | 4524 | const dst = try func.resolveInst(pl_op.operand); |
| 4527 | const src = try self.resolveInst(bin_op.lhs); | 4525 | const src = try func.resolveInst(bin_op.lhs); |
| 4528 | const len = try self.resolveInst(bin_op.rhs); | 4526 | const len = try func.resolveInst(bin_op.rhs); |
| 4529 | try self.memcpy(dst, src, len); | 4527 | try func.memcpy(dst, src, len); |
| 4530 | | 4528 | |
| 4531 | self.finishAir(inst, .none, &.{pl_op.operand}); | 4529 | func.finishAir(inst, .none, &.{pl_op.operand}); |
| 4532 | } | 4530 | } |
| 4533 | | 4531 | |
| 4534 | fn airPopcount(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4532 | fn airPopcount(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4535 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 4533 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 4536 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 4534 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 4537 | | 4535 | |
| 4538 | const operand = try self.resolveInst(ty_op.operand); | 4536 | const operand = try func.resolveInst(ty_op.operand); |
| 4539 | const op_ty = self.air.typeOf(ty_op.operand); | 4537 | const op_ty = func.air.typeOf(ty_op.operand); |
| 4540 | const result_ty = self.air.typeOfIndex(inst); | 4538 | const result_ty = func.air.typeOfIndex(inst); |
| 4541 | | 4539 | |
| 4542 | if (op_ty.zigTypeTag() == .Vector) { | 4540 | if (op_ty.zigTypeTag() == .Vector) { |
| 4543 | return self.fail("TODO: Implement @popCount for vectors", .{}); | 4541 | return func.fail("TODO: Implement @popCount for vectors", .{}); |
| 4544 | } | 4542 | } |
| 4545 | | 4543 | |
| 4546 | const int_info = op_ty.intInfo(self.target); | 4544 | const int_info = op_ty.intInfo(func.target); |
| 4547 | const bits = int_info.bits; | 4545 | const bits = int_info.bits; |
| 4548 | const wasm_bits = toWasmBits(bits) orelse { | 4546 | const wasm_bits = toWasmBits(bits) orelse { |
| 4549 | return self.fail("TODO: Implement @popCount for integers with bitsize '{d}'", .{bits}); | 4547 | return func.fail("TODO: Implement @popCount for integers with bitsize '{d}'", .{bits}); |
| 4550 | }; | 4548 | }; |
| 4551 | | 4549 | |
| 4552 | switch (wasm_bits) { | 4550 | switch (wasm_bits) { |
| 4553 | 128 => { | 4551 | 128 => { |
| 4554 | _ = try self.load(operand, Type.u64, 0); | 4552 | _ = try func.load(operand, Type.u64, 0); |
| 4555 | try self.addTag(.i64_popcnt); | 4553 | try func.addTag(.i64_popcnt); |
| 4556 | _ = try self.load(operand, Type.u64, 8); | 4554 | _ = try func.load(operand, Type.u64, 8); |
| 4557 | try self.addTag(.i64_popcnt); | 4555 | try func.addTag(.i64_popcnt); |
| 4558 | try self.addTag(.i64_add); | 4556 | try func.addTag(.i64_add); |
| 4559 | try self.addTag(.i32_wrap_i64); | 4557 | try func.addTag(.i32_wrap_i64); |
| 4560 | }, | 4558 | }, |
| 4561 | else => { | 4559 | else => { |
| 4562 | try self.emitWValue(operand); | 4560 | try func.emitWValue(operand); |
| 4563 | switch (wasm_bits) { | 4561 | switch (wasm_bits) { |
| 4564 | 32 => try self.addTag(.i32_popcnt), | 4562 | 32 => try func.addTag(.i32_popcnt), |
| 4565 | 64 => { | 4563 | 64 => { |
| 4566 | try self.addTag(.i64_popcnt); | 4564 | try func.addTag(.i64_popcnt); |
| 4567 | try self.addTag(.i32_wrap_i64); | 4565 | try func.addTag(.i32_wrap_i64); |
| 4568 | }, | 4566 | }, |
| 4569 | else => unreachable, | 4567 | else => unreachable, |
| 4570 | } | 4568 | } |
| 4571 | }, | 4569 | }, |
| 4572 | } | 4570 | } |
| 4573 | | 4571 | |
| 4574 | const result = try self.allocLocal(result_ty); | 4572 | const result = try func.allocLocal(result_ty); |
| 4575 | try self.addLabel(.local_set, result.local.value); | 4573 | try func.addLabel(.local_set, result.local.value); |
| 4576 | self.finishAir(inst, result, &.{ty_op.operand}); | 4574 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4577 | } | 4575 | } |
| 4578 | | 4576 | |
| 4579 | fn airErrorName(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4577 | fn airErrorName(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4580 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 4578 | const un_op = func.air.instructions.items(.data)[inst].un_op; |
| 4581 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op}); | 4579 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{un_op}); |
| 4582 | | 4580 | |
| 4583 | const operand = try self.resolveInst(un_op); | 4581 | const operand = try func.resolveInst(un_op); |
| 4584 | // First retrieve the symbol index to the error name table | 4582 | // First retrieve the symbol index to the error name table |
| 4585 | // that will be used to emit a relocation for the pointer | 4583 | // that will be used to emit a relocation for the pointer |
| 4586 | // to the error name table. | 4584 | // to the error name table. |
| ... | @@ -4592,63 +4590,63 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -4592,63 +4590,63 @@ fn airErrorName(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4592 | // | 4590 | // |
| 4593 | // As the names are global and the slice elements are constant, we do not have | 4591 | // As the names are global and the slice elements are constant, we do not have |
| 4594 | // to make a copy of the ptr+value but can point towards them directly. | 4592 | // to make a copy of the ptr+value but can point towards them directly. |
| 4595 | const error_table_symbol = try self.bin_file.getErrorTableSymbol(); | 4593 | const error_table_symbol = try func.bin_file.getErrorTableSymbol(); |
| 4596 | const name_ty = Type.initTag(.const_slice_u8_sentinel_0); | 4594 | const name_ty = Type.initTag(.const_slice_u8_sentinel_0); |
| 4597 | const abi_size = name_ty.abiSize(self.target); | 4595 | const abi_size = name_ty.abiSize(func.target); |
| 4598 | | 4596 | |
| 4599 | const error_name_value: WValue = .{ .memory = error_table_symbol }; // emitting this will create a relocation | 4597 | const error_name_value: WValue = .{ .memory = error_table_symbol }; // emitting this will create a relocation |
| 4600 | try self.emitWValue(error_name_value); | 4598 | try func.emitWValue(error_name_value); |
| 4601 | try self.emitWValue(operand); | 4599 | try func.emitWValue(operand); |
| 4602 | switch (self.arch()) { | 4600 | switch (func.arch()) { |
| 4603 | .wasm32 => { | 4601 | .wasm32 => { |
| 4604 | try self.addImm32(@bitCast(i32, @intCast(u32, abi_size))); | 4602 | try func.addImm32(@bitCast(i32, @intCast(u32, abi_size))); |
| 4605 | try self.addTag(.i32_mul); | 4603 | try func.addTag(.i32_mul); |
| 4606 | try self.addTag(.i32_add); | 4604 | try func.addTag(.i32_add); |
| 4607 | }, | 4605 | }, |
| 4608 | .wasm64 => { | 4606 | .wasm64 => { |
| 4609 | try self.addImm64(abi_size); | 4607 | try func.addImm64(abi_size); |
| 4610 | try self.addTag(.i64_mul); | 4608 | try func.addTag(.i64_mul); |
| 4611 | try self.addTag(.i64_add); | 4609 | try func.addTag(.i64_add); |
| 4612 | }, | 4610 | }, |
| 4613 | else => unreachable, | 4611 | else => unreachable, |
| 4614 | } | 4612 | } |
| 4615 | | 4613 | |
| 4616 | const result_ptr = try self.allocLocal(Type.usize); | 4614 | const result_ptr = try func.allocLocal(Type.usize); |
| 4617 | try self.addLabel(.local_set, result_ptr.local.value); | 4615 | try func.addLabel(.local_set, result_ptr.local.value); |
| 4618 | self.finishAir(inst, result_ptr, &.{un_op}); | 4616 | func.finishAir(inst, result_ptr, &.{un_op}); |
| 4619 | } | 4617 | } |
| 4620 | | 4618 | |
| 4621 | fn airPtrSliceFieldPtr(self: *Self, inst: Air.Inst.Index, offset: u32) InnerError!void { | 4619 | fn airPtrSliceFieldPtr(func: *CodeGen, inst: Air.Inst.Index, offset: u32) InnerError!void { |
| 4622 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 4620 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 4623 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 4621 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 4624 | const slice_ptr = try self.resolveInst(ty_op.operand); | 4622 | const slice_ptr = try func.resolveInst(ty_op.operand); |
| 4625 | const result = try self.buildPointerOffset(slice_ptr, offset, .new); | 4623 | const result = try func.buildPointerOffset(slice_ptr, offset, .new); |
| 4626 | self.finishAir(inst, result, &.{ty_op.operand}); | 4624 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 4627 | } | 4625 | } |
| 4628 | | 4626 | |
| 4629 | fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { | 4627 | fn airAddSubWithOverflow(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 4630 | assert(op == .add or op == .sub); | 4628 | assert(op == .add or op == .sub); |
| 4631 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 4629 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 4632 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; | 4630 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4633 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ extra.lhs, extra.rhs }); | 4631 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ extra.lhs, extra.rhs }); |
| 4634 | | 4632 | |
| 4635 | const lhs_op = try self.resolveInst(extra.lhs); | 4633 | const lhs_op = try func.resolveInst(extra.lhs); |
| 4636 | const rhs_op = try self.resolveInst(extra.rhs); | 4634 | const rhs_op = try func.resolveInst(extra.rhs); |
| 4637 | const lhs_ty = self.air.typeOf(extra.lhs); | 4635 | const lhs_ty = func.air.typeOf(extra.lhs); |
| 4638 | | 4636 | |
| 4639 | if (lhs_ty.zigTypeTag() == .Vector) { | 4637 | if (lhs_ty.zigTypeTag() == .Vector) { |
| 4640 | return self.fail("TODO: Implement overflow arithmetic for vectors", .{}); | 4638 | return func.fail("TODO: Implement overflow arithmetic for vectors", .{}); |
| 4641 | } | 4639 | } |
| 4642 | | 4640 | |
| 4643 | const int_info = lhs_ty.intInfo(self.target); | 4641 | const int_info = lhs_ty.intInfo(func.target); |
| 4644 | const is_signed = int_info.signedness == .signed; | 4642 | const is_signed = int_info.signedness == .signed; |
| 4645 | const wasm_bits = toWasmBits(int_info.bits) orelse { | 4643 | const wasm_bits = toWasmBits(int_info.bits) orelse { |
| 4646 | return self.fail("TODO: Implement {{add/sub}}_with_overflow for integer bitsize: {d}", .{int_info.bits}); | 4644 | return func.fail("TODO: Implement {{add/sub}}_with_overflow for integer bitsize: {d}", .{int_info.bits}); |
| 4647 | }; | 4645 | }; |
| 4648 | | 4646 | |
| 4649 | if (wasm_bits == 128) { | 4647 | if (wasm_bits == 128) { |
| 4650 | const result = try self.addSubWithOverflowBigInt(lhs_op, rhs_op, lhs_ty, self.air.typeOfIndex(inst), op); | 4648 | const result = try func.addSubWithOverflowBigInt(lhs_op, rhs_op, lhs_ty, func.air.typeOfIndex(inst), op); |
| 4651 | return self.finishAir(inst, result, &.{ extra.lhs, extra.rhs }); | 4649 | return func.finishAir(inst, result, &.{ extra.lhs, extra.rhs }); |
| 4652 | } | 4650 | } |
| 4653 | | 4651 | |
| 4654 | const zero = switch (wasm_bits) { | 4652 | const zero = switch (wasm_bits) { |
| ... | @@ -4660,10 +4658,10 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!v | ... | @@ -4660,10 +4658,10 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!v |
| 4660 | // for signed integers, we first apply signed shifts by the difference in bits | 4658 | // for signed integers, we first apply signed shifts by the difference in bits |
| 4661 | // to get the signed value, as we store it internally as 2's complement. | 4659 | // to get the signed value, as we store it internally as 2's complement. |
| 4662 | var lhs = if (wasm_bits != int_info.bits and is_signed) blk: { | 4660 | var lhs = if (wasm_bits != int_info.bits and is_signed) blk: { |
| 4663 | break :blk try (try self.signAbsValue(lhs_op, lhs_ty)).toLocal(self, lhs_ty); | 4661 | break :blk try (try func.signAbsValue(lhs_op, lhs_ty)).toLocal(func, lhs_ty); |
| 4664 | } else lhs_op; | 4662 | } else lhs_op; |
| 4665 | var rhs = if (wasm_bits != int_info.bits and is_signed) blk: { | 4663 | var rhs = if (wasm_bits != int_info.bits and is_signed) blk: { |
| 4666 | break :blk try (try self.signAbsValue(rhs_op, lhs_ty)).toLocal(self, lhs_ty); | 4664 | break :blk try (try func.signAbsValue(rhs_op, lhs_ty)).toLocal(func, lhs_ty); |
| 4667 | } else rhs_op; | 4665 | } else rhs_op; |
| 4668 | | 4666 | |
| 4669 | // in this case, we performed a signAbsValue which created a temporary local | 4667 | // in this case, we performed a signAbsValue which created a temporary local |
| ... | @@ -4671,178 +4669,178 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!v | ... | @@ -4671,178 +4669,178 @@ fn airAddSubWithOverflow(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!v |
| 4671 | // In the other case we do not want to free it, because that would free the | 4669 | // In the other case we do not want to free it, because that would free the |
| 4672 | // resolved instructions which may be referenced by other instructions. | 4670 | // resolved instructions which may be referenced by other instructions. |
| 4673 | defer if (wasm_bits != int_info.bits and is_signed) { | 4671 | defer if (wasm_bits != int_info.bits and is_signed) { |
| 4674 | lhs.free(self); | 4672 | lhs.free(func); |
| 4675 | rhs.free(self); | 4673 | rhs.free(func); |
| 4676 | }; | 4674 | }; |
| 4677 | | 4675 | |
| 4678 | var bin_op = try (try self.binOp(lhs, rhs, lhs_ty, op)).toLocal(self, lhs_ty); | 4676 | var bin_op = try (try func.binOp(lhs, rhs, lhs_ty, op)).toLocal(func, lhs_ty); |
| 4679 | defer bin_op.free(self); | 4677 | defer bin_op.free(func); |
| 4680 | var result = if (wasm_bits != int_info.bits) blk: { | 4678 | var result = if (wasm_bits != int_info.bits) blk: { |
| 4681 | break :blk try (try self.wrapOperand(bin_op, lhs_ty)).toLocal(self, lhs_ty); | 4679 | break :blk try (try func.wrapOperand(bin_op, lhs_ty)).toLocal(func, lhs_ty); |
| 4682 | } else bin_op; | 4680 | } else bin_op; |
| 4683 | defer result.free(self); // no-op when wasm_bits == int_info.bits | 4681 | defer result.free(func); // no-op when wasm_bits == int_info.bits |
| 4684 | | 4682 | |
| 4685 | const cmp_op: std.math.CompareOperator = if (op == .sub) .gt else .lt; | 4683 | const cmp_op: std.math.CompareOperator = if (op == .sub) .gt else .lt; |
| 4686 | const overflow_bit: WValue = if (is_signed) blk: { | 4684 | const overflow_bit: WValue = if (is_signed) blk: { |
| 4687 | if (wasm_bits == int_info.bits) { | 4685 | if (wasm_bits == int_info.bits) { |
| 4688 | const cmp_zero = try self.cmp(rhs, zero, lhs_ty, cmp_op); | 4686 | const cmp_zero = try func.cmp(rhs, zero, lhs_ty, cmp_op); |
| 4689 | const lt = try self.cmp(bin_op, lhs, lhs_ty, .lt); | 4687 | const lt = try func.cmp(bin_op, lhs, lhs_ty, .lt); |
| 4690 | break :blk try self.binOp(cmp_zero, lt, Type.u32, .xor); | 4688 | break :blk try func.binOp(cmp_zero, lt, Type.u32, .xor); |
| 4691 | } | 4689 | } |
| 4692 | const abs = try self.signAbsValue(bin_op, lhs_ty); | 4690 | const abs = try func.signAbsValue(bin_op, lhs_ty); |
| 4693 | break :blk try self.cmp(abs, bin_op, lhs_ty, .neq); | 4691 | break :blk try func.cmp(abs, bin_op, lhs_ty, .neq); |
| 4694 | } else if (wasm_bits == int_info.bits) | 4692 | } else if (wasm_bits == int_info.bits) |
| 4695 | try self.cmp(bin_op, lhs, lhs_ty, cmp_op) | 4693 | try func.cmp(bin_op, lhs, lhs_ty, cmp_op) |
| 4696 | else | 4694 | else |
| 4697 | try self.cmp(bin_op, result, lhs_ty, .neq); | 4695 | try func.cmp(bin_op, result, lhs_ty, .neq); |
| 4698 | var overflow_local = try overflow_bit.toLocal(self, Type.u32); | 4696 | var overflow_local = try overflow_bit.toLocal(func, Type.u32); |
| 4699 | defer overflow_local.free(self); | 4697 | defer overflow_local.free(func); |
| 4700 | | 4698 | |
| 4701 | const result_ptr = try self.allocStack(self.air.typeOfIndex(inst)); | 4699 | const result_ptr = try func.allocStack(func.air.typeOfIndex(inst)); |
| 4702 | try self.store(result_ptr, result, lhs_ty, 0); | 4700 | try func.store(result_ptr, result, lhs_ty, 0); |
| 4703 | const offset = @intCast(u32, lhs_ty.abiSize(self.target)); | 4701 | const offset = @intCast(u32, lhs_ty.abiSize(func.target)); |
| 4704 | try self.store(result_ptr, overflow_local, Type.initTag(.u1), offset); | 4702 | try func.store(result_ptr, overflow_local, Type.initTag(.u1), offset); |
| 4705 | | 4703 | |
| 4706 | self.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); | 4704 | func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| 4707 | } | 4705 | } |
| 4708 | | 4706 | |
| 4709 | fn addSubWithOverflowBigInt(self: *Self, lhs: WValue, rhs: WValue, ty: Type, result_ty: Type, op: Op) InnerError!WValue { | 4707 | fn addSubWithOverflowBigInt(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type, result_ty: Type, op: Op) InnerError!WValue { |
| 4710 | assert(op == .add or op == .sub); | 4708 | assert(op == .add or op == .sub); |
| 4711 | const int_info = ty.intInfo(self.target); | 4709 | const int_info = ty.intInfo(func.target); |
| 4712 | const is_signed = int_info.signedness == .signed; | 4710 | const is_signed = int_info.signedness == .signed; |
| 4713 | if (int_info.bits != 128) { | 4711 | if (int_info.bits != 128) { |
| 4714 | return self.fail("TODO: Implement @{{add/sub}}WithOverflow for integer bitsize '{d}'", .{int_info.bits}); | 4712 | return func.fail("TODO: Implement @{{add/sub}}WithOverflow for integer bitsize '{d}'", .{int_info.bits}); |
| 4715 | } | 4713 | } |
| 4716 | | 4714 | |
| 4717 | var lhs_high_bit = try (try self.load(lhs, Type.u64, 0)).toLocal(self, Type.u64); | 4715 | var lhs_high_bit = try (try func.load(lhs, Type.u64, 0)).toLocal(func, Type.u64); |
| 4718 | defer lhs_high_bit.free(self); | 4716 | defer lhs_high_bit.free(func); |
| 4719 | var lhs_low_bit = try (try self.load(lhs, Type.u64, 8)).toLocal(self, Type.u64); | 4717 | var lhs_low_bit = try (try func.load(lhs, Type.u64, 8)).toLocal(func, Type.u64); |
| 4720 | defer lhs_low_bit.free(self); | 4718 | defer lhs_low_bit.free(func); |
| 4721 | var rhs_high_bit = try (try self.load(rhs, Type.u64, 0)).toLocal(self, Type.u64); | 4719 | var rhs_high_bit = try (try func.load(rhs, Type.u64, 0)).toLocal(func, Type.u64); |
| 4722 | defer rhs_high_bit.free(self); | 4720 | defer rhs_high_bit.free(func); |
| 4723 | var rhs_low_bit = try (try self.load(rhs, Type.u64, 8)).toLocal(self, Type.u64); | 4721 | var rhs_low_bit = try (try func.load(rhs, Type.u64, 8)).toLocal(func, Type.u64); |
| 4724 | defer rhs_low_bit.free(self); | 4722 | defer rhs_low_bit.free(func); |
| 4725 | | 4723 | |
| 4726 | var low_op_res = try (try self.binOp(lhs_low_bit, rhs_low_bit, Type.u64, op)).toLocal(self, Type.u64); | 4724 | var low_op_res = try (try func.binOp(lhs_low_bit, rhs_low_bit, Type.u64, op)).toLocal(func, Type.u64); |
| 4727 | defer low_op_res.free(self); | 4725 | defer low_op_res.free(func); |
| 4728 | var high_op_res = try (try self.binOp(lhs_high_bit, rhs_high_bit, Type.u64, op)).toLocal(self, Type.u64); | 4726 | var high_op_res = try (try func.binOp(lhs_high_bit, rhs_high_bit, Type.u64, op)).toLocal(func, Type.u64); |
| 4729 | defer high_op_res.free(self); | 4727 | defer high_op_res.free(func); |
| 4730 | | 4728 | |
| 4731 | var lt = if (op == .add) blk: { | 4729 | var lt = if (op == .add) blk: { |
| 4732 | break :blk try (try self.cmp(high_op_res, lhs_high_bit, Type.u64, .lt)).toLocal(self, Type.u32); | 4730 | break :blk try (try func.cmp(high_op_res, lhs_high_bit, Type.u64, .lt)).toLocal(func, Type.u32); |
| 4733 | } else if (op == .sub) blk: { | 4731 | } else if (op == .sub) blk: { |
| 4734 | break :blk try (try self.cmp(lhs_high_bit, rhs_high_bit, Type.u64, .lt)).toLocal(self, Type.u32); | 4732 | break :blk try (try func.cmp(lhs_high_bit, rhs_high_bit, Type.u64, .lt)).toLocal(func, Type.u32); |
| 4735 | } else unreachable; | 4733 | } else unreachable; |
| 4736 | defer lt.free(self); | 4734 | defer lt.free(func); |
| 4737 | var tmp = try (try self.intcast(lt, Type.u32, Type.u64)).toLocal(self, Type.u64); | 4735 | var tmp = try (try func.intcast(lt, Type.u32, Type.u64)).toLocal(func, Type.u64); |
| 4738 | defer tmp.free(self); | 4736 | defer tmp.free(func); |
| 4739 | var tmp_op = try (try self.binOp(low_op_res, tmp, Type.u64, op)).toLocal(self, Type.u64); | 4737 | var tmp_op = try (try func.binOp(low_op_res, tmp, Type.u64, op)).toLocal(func, Type.u64); |
| 4740 | defer tmp_op.free(self); | 4738 | defer tmp_op.free(func); |
| 4741 | | 4739 | |
| 4742 | const overflow_bit = if (is_signed) blk: { | 4740 | const overflow_bit = if (is_signed) blk: { |
| 4743 | const xor_low = try self.binOp(lhs_low_bit, rhs_low_bit, Type.u64, .xor); | 4741 | const xor_low = try func.binOp(lhs_low_bit, rhs_low_bit, Type.u64, .xor); |
| 4744 | const to_wrap = if (op == .add) wrap: { | 4742 | const to_wrap = if (op == .add) wrap: { |
| 4745 | break :wrap try self.binOp(xor_low, .{ .imm64 = ~@as(u64, 0) }, Type.u64, .xor); | 4743 | break :wrap try func.binOp(xor_low, .{ .imm64 = ~@as(u64, 0) }, Type.u64, .xor); |
| 4746 | } else xor_low; | 4744 | } else xor_low; |
| 4747 | const xor_op = try self.binOp(lhs_low_bit, tmp_op, Type.u64, .xor); | 4745 | const xor_op = try func.binOp(lhs_low_bit, tmp_op, Type.u64, .xor); |
| 4748 | const wrap = try self.binOp(to_wrap, xor_op, Type.u64, .@"and"); | 4746 | const wrap = try func.binOp(to_wrap, xor_op, Type.u64, .@"and"); |
| 4749 | break :blk try self.cmp(wrap, .{ .imm64 = 0 }, Type.i64, .lt); // i64 because signed | 4747 | break :blk try func.cmp(wrap, .{ .imm64 = 0 }, Type.i64, .lt); // i64 because signed |
| 4750 | } else blk: { | 4748 | } else blk: { |
| 4751 | const first_arg = if (op == .sub) arg: { | 4749 | const first_arg = if (op == .sub) arg: { |
| 4752 | break :arg try self.cmp(high_op_res, lhs_high_bit, Type.u64, .gt); | 4750 | break :arg try func.cmp(high_op_res, lhs_high_bit, Type.u64, .gt); |
| 4753 | } else lt; | 4751 | } else lt; |
| 4754 | | 4752 | |
| 4755 | try self.emitWValue(first_arg); | 4753 | try func.emitWValue(first_arg); |
| 4756 | _ = try self.cmp(tmp_op, lhs_low_bit, Type.u64, if (op == .add) .lt else .gt); | 4754 | _ = try func.cmp(tmp_op, lhs_low_bit, Type.u64, if (op == .add) .lt else .gt); |
| 4757 | _ = try self.cmp(tmp_op, lhs_low_bit, Type.u64, .eq); | 4755 | _ = try func.cmp(tmp_op, lhs_low_bit, Type.u64, .eq); |
| 4758 | try self.addTag(.select); | 4756 | try func.addTag(.select); |
| 4759 | | 4757 | |
| 4760 | break :blk WValue{ .stack = {} }; | 4758 | break :blk WValue{ .stack = {} }; |
| 4761 | }; | 4759 | }; |
| 4762 | var overflow_local = try overflow_bit.toLocal(self, Type.initTag(.u1)); | 4760 | var overflow_local = try overflow_bit.toLocal(func, Type.initTag(.u1)); |
| 4763 | defer overflow_local.free(self); | 4761 | defer overflow_local.free(func); |
| 4764 | | 4762 | |
| 4765 | const result_ptr = try self.allocStack(result_ty); | 4763 | const result_ptr = try func.allocStack(result_ty); |
| 4766 | try self.store(result_ptr, high_op_res, Type.u64, 0); | 4764 | try func.store(result_ptr, high_op_res, Type.u64, 0); |
| 4767 | try self.store(result_ptr, tmp_op, Type.u64, 8); | 4765 | try func.store(result_ptr, tmp_op, Type.u64, 8); |
| 4768 | try self.store(result_ptr, overflow_local, Type.initTag(.u1), 16); | 4766 | try func.store(result_ptr, overflow_local, Type.initTag(.u1), 16); |
| 4769 | | 4767 | |
| 4770 | return result_ptr; | 4768 | return result_ptr; |
| 4771 | } | 4769 | } |
| 4772 | | 4770 | |
| 4773 | fn airShlWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4771 | fn airShlWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4774 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 4772 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 4775 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; | 4773 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4776 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ extra.lhs, extra.rhs }); | 4774 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ extra.lhs, extra.rhs }); |
| 4777 | | 4775 | |
| 4778 | const lhs = try self.resolveInst(extra.lhs); | 4776 | const lhs = try func.resolveInst(extra.lhs); |
| 4779 | const rhs = try self.resolveInst(extra.rhs); | 4777 | const rhs = try func.resolveInst(extra.rhs); |
| 4780 | const lhs_ty = self.air.typeOf(extra.lhs); | 4778 | const lhs_ty = func.air.typeOf(extra.lhs); |
| 4781 | | 4779 | |
| 4782 | if (lhs_ty.zigTypeTag() == .Vector) { | 4780 | if (lhs_ty.zigTypeTag() == .Vector) { |
| 4783 | return self.fail("TODO: Implement overflow arithmetic for vectors", .{}); | 4781 | return func.fail("TODO: Implement overflow arithmetic for vectors", .{}); |
| 4784 | } | 4782 | } |
| 4785 | | 4783 | |
| 4786 | const int_info = lhs_ty.intInfo(self.target); | 4784 | const int_info = lhs_ty.intInfo(func.target); |
| 4787 | const is_signed = int_info.signedness == .signed; | 4785 | const is_signed = int_info.signedness == .signed; |
| 4788 | const wasm_bits = toWasmBits(int_info.bits) orelse { | 4786 | const wasm_bits = toWasmBits(int_info.bits) orelse { |
| 4789 | return self.fail("TODO: Implement shl_with_overflow for integer bitsize: {d}", .{int_info.bits}); | 4787 | return func.fail("TODO: Implement shl_with_overflow for integer bitsize: {d}", .{int_info.bits}); |
| 4790 | }; | 4788 | }; |
| 4791 | | 4789 | |
| 4792 | var shl = try (try self.binOp(lhs, rhs, lhs_ty, .shl)).toLocal(self, lhs_ty); | 4790 | var shl = try (try func.binOp(lhs, rhs, lhs_ty, .shl)).toLocal(func, lhs_ty); |
| 4793 | defer shl.free(self); | 4791 | defer shl.free(func); |
| 4794 | var result = if (wasm_bits != int_info.bits) blk: { | 4792 | var result = if (wasm_bits != int_info.bits) blk: { |
| 4795 | break :blk try (try self.wrapOperand(shl, lhs_ty)).toLocal(self, lhs_ty); | 4793 | break :blk try (try func.wrapOperand(shl, lhs_ty)).toLocal(func, lhs_ty); |
| 4796 | } else shl; | 4794 | } else shl; |
| 4797 | defer result.free(self); // it's a no-op to free the same local twice (when wasm_bits == int_info.bits) | 4795 | defer result.free(func); // it's a no-op to free the same local twice (when wasm_bits == int_info.bits) |
| 4798 | | 4796 | |
| 4799 | const overflow_bit = if (wasm_bits != int_info.bits and is_signed) blk: { | 4797 | const overflow_bit = if (wasm_bits != int_info.bits and is_signed) blk: { |
| 4800 | // emit lhs to stack to we can keep 'wrapped' on the stack also | 4798 | // emit lhs to stack to we can keep 'wrapped' on the stack also |
| 4801 | try self.emitWValue(lhs); | 4799 | try func.emitWValue(lhs); |
| 4802 | const abs = try self.signAbsValue(shl, lhs_ty); | 4800 | const abs = try func.signAbsValue(shl, lhs_ty); |
| 4803 | const wrapped = try self.wrapBinOp(abs, rhs, lhs_ty, .shr); | 4801 | const wrapped = try func.wrapBinOp(abs, rhs, lhs_ty, .shr); |
| 4804 | break :blk try self.cmp(.{ .stack = {} }, wrapped, lhs_ty, .neq); | 4802 | break :blk try func.cmp(.{ .stack = {} }, wrapped, lhs_ty, .neq); |
| 4805 | } else blk: { | 4803 | } else blk: { |
| 4806 | try self.emitWValue(lhs); | 4804 | try func.emitWValue(lhs); |
| 4807 | const shr = try self.binOp(result, rhs, lhs_ty, .shr); | 4805 | const shr = try func.binOp(result, rhs, lhs_ty, .shr); |
| 4808 | break :blk try self.cmp(.{ .stack = {} }, shr, lhs_ty, .neq); | 4806 | break :blk try func.cmp(.{ .stack = {} }, shr, lhs_ty, .neq); |
| 4809 | }; | 4807 | }; |
| 4810 | var overflow_local = try overflow_bit.toLocal(self, Type.initTag(.u1)); | 4808 | var overflow_local = try overflow_bit.toLocal(func, Type.initTag(.u1)); |
| 4811 | defer overflow_local.free(self); | 4809 | defer overflow_local.free(func); |
| 4812 | | 4810 | |
| 4813 | const result_ptr = try self.allocStack(self.air.typeOfIndex(inst)); | 4811 | const result_ptr = try func.allocStack(func.air.typeOfIndex(inst)); |
| 4814 | try self.store(result_ptr, result, lhs_ty, 0); | 4812 | try func.store(result_ptr, result, lhs_ty, 0); |
| 4815 | const offset = @intCast(u32, lhs_ty.abiSize(self.target)); | 4813 | const offset = @intCast(u32, lhs_ty.abiSize(func.target)); |
| 4816 | try self.store(result_ptr, overflow_local, Type.initTag(.u1), offset); | 4814 | try func.store(result_ptr, overflow_local, Type.initTag(.u1), offset); |
| 4817 | | 4815 | |
| 4818 | self.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); | 4816 | func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| 4819 | } | 4817 | } |
| 4820 | | 4818 | |
| 4821 | fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4819 | fn airMulWithOverflow(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4822 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 4820 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 4823 | const extra = self.air.extraData(Air.Bin, ty_pl.payload).data; | 4821 | const extra = func.air.extraData(Air.Bin, ty_pl.payload).data; |
| 4824 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ extra.lhs, extra.rhs }); | 4822 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ extra.lhs, extra.rhs }); |
| 4825 | | 4823 | |
| 4826 | const lhs = try self.resolveInst(extra.lhs); | 4824 | const lhs = try func.resolveInst(extra.lhs); |
| 4827 | const rhs = try self.resolveInst(extra.rhs); | 4825 | const rhs = try func.resolveInst(extra.rhs); |
| 4828 | const lhs_ty = self.air.typeOf(extra.lhs); | 4826 | const lhs_ty = func.air.typeOf(extra.lhs); |
| 4829 | | 4827 | |
| 4830 | if (lhs_ty.zigTypeTag() == .Vector) { | 4828 | if (lhs_ty.zigTypeTag() == .Vector) { |
| 4831 | return self.fail("TODO: Implement overflow arithmetic for vectors", .{}); | 4829 | return func.fail("TODO: Implement overflow arithmetic for vectors", .{}); |
| 4832 | } | 4830 | } |
| 4833 | | 4831 | |
| 4834 | // We store the bit if it's overflowed or not in this. As it's zero-initialized | 4832 | // We store the bit if it's overflowed or not in this. As it's zero-initialized |
| 4835 | // we only need to update it if an overflow (or underflow) occurred. | 4833 | // we only need to update it if an overflow (or underflow) occurred. |
| 4836 | var overflow_bit = try self.ensureAllocLocal(Type.initTag(.u1)); | 4834 | var overflow_bit = try func.ensureAllocLocal(Type.initTag(.u1)); |
| 4837 | defer overflow_bit.free(self); | 4835 | defer overflow_bit.free(func); |
| 4838 | | 4836 | |
| 4839 | const int_info = lhs_ty.intInfo(self.target); | 4837 | const int_info = lhs_ty.intInfo(func.target); |
| 4840 | const wasm_bits = toWasmBits(int_info.bits) orelse { | 4838 | const wasm_bits = toWasmBits(int_info.bits) orelse { |
| 4841 | return self.fail("TODO: Implement overflow arithmetic for integer bitsize: {d}", .{int_info.bits}); | 4839 | return func.fail("TODO: Implement overflow arithmetic for integer bitsize: {d}", .{int_info.bits}); |
| 4842 | }; | 4840 | }; |
| 4843 | | 4841 | |
| 4844 | if (wasm_bits > 32) { | 4842 | if (wasm_bits > 32) { |
| 4845 | return self.fail("TODO: Implement `@mulWithOverflow` for integer bitsize: {d}", .{int_info.bits}); | 4843 | return func.fail("TODO: Implement `@mulWithOverflow` for integer bitsize: {d}", .{int_info.bits}); |
| 4846 | } | 4844 | } |
| 4847 | | 4845 | |
| 4848 | const zero = switch (wasm_bits) { | 4846 | const zero = switch (wasm_bits) { |
| ... | @@ -4854,190 +4852,190 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -4854,190 +4852,190 @@ fn airMulWithOverflow(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 4854 | // for 32 bit integers we upcast it to a 64bit integer | 4852 | // for 32 bit integers we upcast it to a 64bit integer |
| 4855 | const bin_op = if (int_info.bits == 32) blk: { | 4853 | const bin_op = if (int_info.bits == 32) blk: { |
| 4856 | const new_ty = if (int_info.signedness == .signed) Type.i64 else Type.u64; | 4854 | const new_ty = if (int_info.signedness == .signed) Type.i64 else Type.u64; |
| 4857 | const lhs_upcast = try self.intcast(lhs, lhs_ty, new_ty); | 4855 | const lhs_upcast = try func.intcast(lhs, lhs_ty, new_ty); |
| 4858 | const rhs_upcast = try self.intcast(rhs, lhs_ty, new_ty); | 4856 | const rhs_upcast = try func.intcast(rhs, lhs_ty, new_ty); |
| 4859 | const bin_op = try (try self.binOp(lhs_upcast, rhs_upcast, new_ty, .mul)).toLocal(self, new_ty); | 4857 | const bin_op = try (try func.binOp(lhs_upcast, rhs_upcast, new_ty, .mul)).toLocal(func, new_ty); |
| 4860 | if (int_info.signedness == .unsigned) { | 4858 | if (int_info.signedness == .unsigned) { |
| 4861 | const shr = try self.binOp(bin_op, .{ .imm64 = int_info.bits }, new_ty, .shr); | 4859 | const shr = try func.binOp(bin_op, .{ .imm64 = int_info.bits }, new_ty, .shr); |
| 4862 | const wrap = try self.intcast(shr, new_ty, lhs_ty); | 4860 | const wrap = try func.intcast(shr, new_ty, lhs_ty); |
| 4863 | _ = try self.cmp(wrap, zero, lhs_ty, .neq); | 4861 | _ = try func.cmp(wrap, zero, lhs_ty, .neq); |
| 4864 | try self.addLabel(.local_set, overflow_bit.local.value); | 4862 | try func.addLabel(.local_set, overflow_bit.local.value); |
| 4865 | break :blk try self.intcast(bin_op, new_ty, lhs_ty); | 4863 | break :blk try func.intcast(bin_op, new_ty, lhs_ty); |
| 4866 | } else { | 4864 | } else { |
| 4867 | const down_cast = try (try self.intcast(bin_op, new_ty, lhs_ty)).toLocal(self, lhs_ty); | 4865 | const down_cast = try (try func.intcast(bin_op, new_ty, lhs_ty)).toLocal(func, lhs_ty); |
| 4868 | var shr = try (try self.binOp(down_cast, .{ .imm32 = int_info.bits - 1 }, lhs_ty, .shr)).toLocal(self, lhs_ty); | 4866 | var shr = try (try func.binOp(down_cast, .{ .imm32 = int_info.bits - 1 }, lhs_ty, .shr)).toLocal(func, lhs_ty); |
| 4869 | defer shr.free(self); | 4867 | defer shr.free(func); |
| 4870 | | 4868 | |
| 4871 | const shr_res = try self.binOp(bin_op, .{ .imm64 = int_info.bits }, new_ty, .shr); | 4869 | const shr_res = try func.binOp(bin_op, .{ .imm64 = int_info.bits }, new_ty, .shr); |
| 4872 | const down_shr_res = try self.intcast(shr_res, new_ty, lhs_ty); | 4870 | const down_shr_res = try func.intcast(shr_res, new_ty, lhs_ty); |
| 4873 | _ = try self.cmp(down_shr_res, shr, lhs_ty, .neq); | 4871 | _ = try func.cmp(down_shr_res, shr, lhs_ty, .neq); |
| 4874 | try self.addLabel(.local_set, overflow_bit.local.value); | 4872 | try func.addLabel(.local_set, overflow_bit.local.value); |
| 4875 | break :blk down_cast; | 4873 | break :blk down_cast; |
| 4876 | } | 4874 | } |
| 4877 | } else if (int_info.signedness == .signed) blk: { | 4875 | } else if (int_info.signedness == .signed) blk: { |
| 4878 | const lhs_abs = try self.signAbsValue(lhs, lhs_ty); | 4876 | const lhs_abs = try func.signAbsValue(lhs, lhs_ty); |
| 4879 | const rhs_abs = try self.signAbsValue(rhs, lhs_ty); | 4877 | const rhs_abs = try func.signAbsValue(rhs, lhs_ty); |
| 4880 | const bin_op = try (try self.binOp(lhs_abs, rhs_abs, lhs_ty, .mul)).toLocal(self, lhs_ty); | 4878 | const bin_op = try (try func.binOp(lhs_abs, rhs_abs, lhs_ty, .mul)).toLocal(func, lhs_ty); |
| 4881 | const mul_abs = try self.signAbsValue(bin_op, lhs_ty); | 4879 | const mul_abs = try func.signAbsValue(bin_op, lhs_ty); |
| 4882 | _ = try self.cmp(mul_abs, bin_op, lhs_ty, .neq); | 4880 | _ = try func.cmp(mul_abs, bin_op, lhs_ty, .neq); |
| 4883 | try self.addLabel(.local_set, overflow_bit.local.value); | 4881 | try func.addLabel(.local_set, overflow_bit.local.value); |
| 4884 | break :blk try self.wrapOperand(bin_op, lhs_ty); | 4882 | break :blk try func.wrapOperand(bin_op, lhs_ty); |
| 4885 | } else blk: { | 4883 | } else blk: { |
| 4886 | var bin_op = try (try self.binOp(lhs, rhs, lhs_ty, .mul)).toLocal(self, lhs_ty); | 4884 | var bin_op = try (try func.binOp(lhs, rhs, lhs_ty, .mul)).toLocal(func, lhs_ty); |
| 4887 | defer bin_op.free(self); | 4885 | defer bin_op.free(func); |
| 4888 | const shift_imm = if (wasm_bits == 32) | 4886 | const shift_imm = if (wasm_bits == 32) |
| 4889 | WValue{ .imm32 = int_info.bits } | 4887 | WValue{ .imm32 = int_info.bits } |
| 4890 | else | 4888 | else |
| 4891 | WValue{ .imm64 = int_info.bits }; | 4889 | WValue{ .imm64 = int_info.bits }; |
| 4892 | const shr = try self.binOp(bin_op, shift_imm, lhs_ty, .shr); | 4890 | const shr = try func.binOp(bin_op, shift_imm, lhs_ty, .shr); |
| 4893 | _ = try self.cmp(shr, zero, lhs_ty, .neq); | 4891 | _ = try func.cmp(shr, zero, lhs_ty, .neq); |
| 4894 | try self.addLabel(.local_set, overflow_bit.local.value); | 4892 | try func.addLabel(.local_set, overflow_bit.local.value); |
| 4895 | break :blk try self.wrapOperand(bin_op, lhs_ty); | 4893 | break :blk try func.wrapOperand(bin_op, lhs_ty); |
| 4896 | }; | 4894 | }; |
| 4897 | var bin_op_local = try bin_op.toLocal(self, lhs_ty); | 4895 | var bin_op_local = try bin_op.toLocal(func, lhs_ty); |
| 4898 | defer bin_op_local.free(self); | 4896 | defer bin_op_local.free(func); |
| 4899 | | 4897 | |
| 4900 | const result_ptr = try self.allocStack(self.air.typeOfIndex(inst)); | 4898 | const result_ptr = try func.allocStack(func.air.typeOfIndex(inst)); |
| 4901 | try self.store(result_ptr, bin_op_local, lhs_ty, 0); | 4899 | try func.store(result_ptr, bin_op_local, lhs_ty, 0); |
| 4902 | const offset = @intCast(u32, lhs_ty.abiSize(self.target)); | 4900 | const offset = @intCast(u32, lhs_ty.abiSize(func.target)); |
| 4903 | try self.store(result_ptr, overflow_bit, Type.initTag(.u1), offset); | 4901 | try func.store(result_ptr, overflow_bit, Type.initTag(.u1), offset); |
| 4904 | | 4902 | |
| 4905 | self.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); | 4903 | func.finishAir(inst, result_ptr, &.{ extra.lhs, extra.rhs }); |
| 4906 | } | 4904 | } |
| 4907 | | 4905 | |
| 4908 | fn airMaxMin(self: *Self, inst: Air.Inst.Index, op: enum { max, min }) InnerError!void { | 4906 | fn airMaxMin(func: *CodeGen, inst: Air.Inst.Index, op: enum { max, min }) InnerError!void { |
| 4909 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 4907 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 4910 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 4908 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 4911 | | 4909 | |
| 4912 | const ty = self.air.typeOfIndex(inst); | 4910 | const ty = func.air.typeOfIndex(inst); |
| 4913 | if (ty.zigTypeTag() == .Vector) { | 4911 | if (ty.zigTypeTag() == .Vector) { |
| 4914 | return self.fail("TODO: `@maximum` and `@minimum` for vectors", .{}); | 4912 | return func.fail("TODO: `@maximum` and `@minimum` for vectors", .{}); |
| 4915 | } | 4913 | } |
| 4916 | | 4914 | |
| 4917 | if (ty.abiSize(self.target) > 16) { | 4915 | if (ty.abiSize(func.target) > 16) { |
| 4918 | return self.fail("TODO: `@maximum` and `@minimum` for types larger than 16 bytes", .{}); | 4916 | return func.fail("TODO: `@maximum` and `@minimum` for types larger than 16 bytes", .{}); |
| 4919 | } | 4917 | } |
| 4920 | | 4918 | |
| 4921 | const lhs = try self.resolveInst(bin_op.lhs); | 4919 | const lhs = try func.resolveInst(bin_op.lhs); |
| 4922 | const rhs = try self.resolveInst(bin_op.rhs); | 4920 | const rhs = try func.resolveInst(bin_op.rhs); |
| 4923 | | 4921 | |
| 4924 | // operands to select from | 4922 | // operands to select from |
| 4925 | try self.lowerToStack(lhs); | 4923 | try func.lowerToStack(lhs); |
| 4926 | try self.lowerToStack(rhs); | 4924 | try func.lowerToStack(rhs); |
| 4927 | _ = try self.cmp(lhs, rhs, ty, if (op == .max) .gt else .lt); | 4925 | _ = try func.cmp(lhs, rhs, ty, if (op == .max) .gt else .lt); |
| 4928 | | 4926 | |
| 4929 | // based on the result from comparison, return operand 0 or 1. | 4927 | // based on the result from comparison, return operand 0 or 1. |
| 4930 | try self.addTag(.select); | 4928 | try func.addTag(.select); |
| 4931 | | 4929 | |
| 4932 | // store result in local | 4930 | // store result in local |
| 4933 | const result_ty = if (isByRef(ty, self.target)) Type.u32 else ty; | 4931 | const result_ty = if (isByRef(ty, func.target)) Type.u32 else ty; |
| 4934 | const result = try self.allocLocal(result_ty); | 4932 | const result = try func.allocLocal(result_ty); |
| 4935 | try self.addLabel(.local_set, result.local.value); | 4933 | try func.addLabel(.local_set, result.local.value); |
| 4936 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 4934 | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 4937 | } | 4935 | } |
| 4938 | | 4936 | |
| 4939 | fn airMulAdd(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4937 | fn airMulAdd(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4940 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 4938 | const pl_op = func.air.instructions.items(.data)[inst].pl_op; |
| 4941 | const bin_op = self.air.extraData(Air.Bin, pl_op.payload).data; | 4939 | const bin_op = func.air.extraData(Air.Bin, pl_op.payload).data; |
| 4942 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 4940 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 4943 | | 4941 | |
| 4944 | const ty = self.air.typeOfIndex(inst); | 4942 | const ty = func.air.typeOfIndex(inst); |
| 4945 | if (ty.zigTypeTag() == .Vector) { | 4943 | if (ty.zigTypeTag() == .Vector) { |
| 4946 | return self.fail("TODO: `@mulAdd` for vectors", .{}); | 4944 | return func.fail("TODO: `@mulAdd` for vectors", .{}); |
| 4947 | } | 4945 | } |
| 4948 | | 4946 | |
| 4949 | const addend = try self.resolveInst(pl_op.operand); | 4947 | const addend = try func.resolveInst(pl_op.operand); |
| 4950 | const lhs = try self.resolveInst(bin_op.lhs); | 4948 | const lhs = try func.resolveInst(bin_op.lhs); |
| 4951 | const rhs = try self.resolveInst(bin_op.rhs); | 4949 | const rhs = try func.resolveInst(bin_op.rhs); |
| 4952 | | 4950 | |
| 4953 | const result = if (ty.floatBits(self.target) == 16) fl_result: { | 4951 | const result = if (ty.floatBits(func.target) == 16) fl_result: { |
| 4954 | const rhs_ext = try self.fpext(rhs, ty, Type.f32); | 4952 | const rhs_ext = try func.fpext(rhs, ty, Type.f32); |
| 4955 | const lhs_ext = try self.fpext(lhs, ty, Type.f32); | 4953 | const lhs_ext = try func.fpext(lhs, ty, Type.f32); |
| 4956 | const addend_ext = try self.fpext(addend, ty, Type.f32); | 4954 | const addend_ext = try func.fpext(addend, ty, Type.f32); |
| 4957 | // call to compiler-rt `fn fmaf(f32, f32, f32) f32` | 4955 | // call to compiler-rt `fn fmaf(f32, f32, f32) f32` |
| 4958 | var result = try self.callIntrinsic( | 4956 | var result = try func.callIntrinsic( |
| 4959 | "fmaf", | 4957 | "fmaf", |
| 4960 | &.{ Type.f32, Type.f32, Type.f32 }, | 4958 | &.{ Type.f32, Type.f32, Type.f32 }, |
| 4961 | Type.f32, | 4959 | Type.f32, |
| 4962 | &.{ rhs_ext, lhs_ext, addend_ext }, | 4960 | &.{ rhs_ext, lhs_ext, addend_ext }, |
| 4963 | ); | 4961 | ); |
| 4964 | break :fl_result try (try self.fptrunc(result, Type.f32, ty)).toLocal(self, ty); | 4962 | break :fl_result try (try func.fptrunc(result, Type.f32, ty)).toLocal(func, ty); |
| 4965 | } else result: { | 4963 | } else result: { |
| 4966 | const mul_result = try self.binOp(lhs, rhs, ty, .mul); | 4964 | const mul_result = try func.binOp(lhs, rhs, ty, .mul); |
| 4967 | break :result try (try self.binOp(mul_result, addend, ty, .add)).toLocal(self, ty); | 4965 | break :result try (try func.binOp(mul_result, addend, ty, .add)).toLocal(func, ty); |
| 4968 | }; | 4966 | }; |
| 4969 | | 4967 | |
| 4970 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 4968 | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 4971 | } | 4969 | } |
| 4972 | | 4970 | |
| 4973 | fn airClz(self: *Self, inst: Air.Inst.Index) InnerError!void { | 4971 | fn airClz(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 4974 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 4972 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 4975 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 4973 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 4976 | | 4974 | |
| 4977 | const ty = self.air.typeOf(ty_op.operand); | 4975 | const ty = func.air.typeOf(ty_op.operand); |
| 4978 | const result_ty = self.air.typeOfIndex(inst); | 4976 | const result_ty = func.air.typeOfIndex(inst); |
| 4979 | if (ty.zigTypeTag() == .Vector) { | 4977 | if (ty.zigTypeTag() == .Vector) { |
| 4980 | return self.fail("TODO: `@clz` for vectors", .{}); | 4978 | return func.fail("TODO: `@clz` for vectors", .{}); |
| 4981 | } | 4979 | } |
| 4982 | | 4980 | |
| 4983 | const operand = try self.resolveInst(ty_op.operand); | 4981 | const operand = try func.resolveInst(ty_op.operand); |
| 4984 | const int_info = ty.intInfo(self.target); | 4982 | const int_info = ty.intInfo(func.target); |
| 4985 | const wasm_bits = toWasmBits(int_info.bits) orelse { | 4983 | const wasm_bits = toWasmBits(int_info.bits) orelse { |
| 4986 | return self.fail("TODO: `@clz` for integers with bitsize '{d}'", .{int_info.bits}); | 4984 | return func.fail("TODO: `@clz` for integers with bitsize '{d}'", .{int_info.bits}); |
| 4987 | }; | 4985 | }; |
| 4988 | | 4986 | |
| 4989 | switch (wasm_bits) { | 4987 | switch (wasm_bits) { |
| 4990 | 32 => { | 4988 | 32 => { |
| 4991 | try self.emitWValue(operand); | 4989 | try func.emitWValue(operand); |
| 4992 | try self.addTag(.i32_clz); | 4990 | try func.addTag(.i32_clz); |
| 4993 | }, | 4991 | }, |
| 4994 | 64 => { | 4992 | 64 => { |
| 4995 | try self.emitWValue(operand); | 4993 | try func.emitWValue(operand); |
| 4996 | try self.addTag(.i64_clz); | 4994 | try func.addTag(.i64_clz); |
| 4997 | try self.addTag(.i32_wrap_i64); | 4995 | try func.addTag(.i32_wrap_i64); |
| 4998 | }, | 4996 | }, |
| 4999 | 128 => { | 4997 | 128 => { |
| 5000 | var lsb = try (try self.load(operand, Type.u64, 8)).toLocal(self, Type.u64); | 4998 | var lsb = try (try func.load(operand, Type.u64, 8)).toLocal(func, Type.u64); |
| 5001 | defer lsb.free(self); | 4999 | defer lsb.free(func); |
| 5002 | | 5000 | |
| 5003 | try self.emitWValue(lsb); | 5001 | try func.emitWValue(lsb); |
| 5004 | try self.addTag(.i64_clz); | 5002 | try func.addTag(.i64_clz); |
| 5005 | _ = try self.load(operand, Type.u64, 0); | 5003 | _ = try func.load(operand, Type.u64, 0); |
| 5006 | try self.addTag(.i64_clz); | 5004 | try func.addTag(.i64_clz); |
| 5007 | try self.emitWValue(.{ .imm64 = 64 }); | 5005 | try func.emitWValue(.{ .imm64 = 64 }); |
| 5008 | try self.addTag(.i64_add); | 5006 | try func.addTag(.i64_add); |
| 5009 | _ = try self.cmp(lsb, .{ .imm64 = 0 }, Type.u64, .neq); | 5007 | _ = try func.cmp(lsb, .{ .imm64 = 0 }, Type.u64, .neq); |
| 5010 | try self.addTag(.select); | 5008 | try func.addTag(.select); |
| 5011 | try self.addTag(.i32_wrap_i64); | 5009 | try func.addTag(.i32_wrap_i64); |
| 5012 | }, | 5010 | }, |
| 5013 | else => unreachable, | 5011 | else => unreachable, |
| 5014 | } | 5012 | } |
| 5015 | | 5013 | |
| 5016 | if (wasm_bits != int_info.bits) { | 5014 | if (wasm_bits != int_info.bits) { |
| 5017 | try self.emitWValue(.{ .imm32 = wasm_bits - int_info.bits }); | 5015 | try func.emitWValue(.{ .imm32 = wasm_bits - int_info.bits }); |
| 5018 | try self.addTag(.i32_sub); | 5016 | try func.addTag(.i32_sub); |
| 5019 | } | 5017 | } |
| 5020 | | 5018 | |
| 5021 | const result = try self.allocLocal(result_ty); | 5019 | const result = try func.allocLocal(result_ty); |
| 5022 | try self.addLabel(.local_set, result.local.value); | 5020 | try func.addLabel(.local_set, result.local.value); |
| 5023 | self.finishAir(inst, result, &.{ty_op.operand}); | 5021 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5024 | } | 5022 | } |
| 5025 | | 5023 | |
| 5026 | fn airCtz(self: *Self, inst: Air.Inst.Index) InnerError!void { | 5024 | fn airCtz(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5027 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 5025 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 5028 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 5026 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 5029 | | 5027 | |
| 5030 | const ty = self.air.typeOf(ty_op.operand); | 5028 | const ty = func.air.typeOf(ty_op.operand); |
| 5031 | const result_ty = self.air.typeOfIndex(inst); | 5029 | const result_ty = func.air.typeOfIndex(inst); |
| 5032 | | 5030 | |
| 5033 | if (ty.zigTypeTag() == .Vector) { | 5031 | if (ty.zigTypeTag() == .Vector) { |
| 5034 | return self.fail("TODO: `@ctz` for vectors", .{}); | 5032 | return func.fail("TODO: `@ctz` for vectors", .{}); |
| 5035 | } | 5033 | } |
| 5036 | | 5034 | |
| 5037 | const operand = try self.resolveInst(ty_op.operand); | 5035 | const operand = try func.resolveInst(ty_op.operand); |
| 5038 | const int_info = ty.intInfo(self.target); | 5036 | const int_info = ty.intInfo(func.target); |
| 5039 | const wasm_bits = toWasmBits(int_info.bits) orelse { | 5037 | const wasm_bits = toWasmBits(int_info.bits) orelse { |
| 5040 | return self.fail("TODO: `@clz` for integers with bitsize '{d}'", .{int_info.bits}); | 5038 | return func.fail("TODO: `@clz` for integers with bitsize '{d}'", .{int_info.bits}); |
| 5041 | }; | 5039 | }; |
| 5042 | | 5040 | |
| 5043 | switch (wasm_bits) { | 5041 | switch (wasm_bits) { |
| ... | @@ -5045,63 +5043,63 @@ fn airCtz(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -5045,63 +5043,63 @@ fn airCtz(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 5045 | if (wasm_bits != int_info.bits) { | 5043 | if (wasm_bits != int_info.bits) { |
| 5046 | const val: u32 = @as(u32, 1) << @intCast(u5, int_info.bits); | 5044 | const val: u32 = @as(u32, 1) << @intCast(u5, int_info.bits); |
| 5047 | // leave value on the stack | 5045 | // leave value on the stack |
| 5048 | _ = try self.binOp(operand, .{ .imm32 = val }, ty, .@"or"); | 5046 | _ = try func.binOp(operand, .{ .imm32 = val }, ty, .@"or"); |
| 5049 | } else try self.emitWValue(operand); | 5047 | } else try func.emitWValue(operand); |
| 5050 | try self.addTag(.i32_ctz); | 5048 | try func.addTag(.i32_ctz); |
| 5051 | }, | 5049 | }, |
| 5052 | 64 => { | 5050 | 64 => { |
| 5053 | if (wasm_bits != int_info.bits) { | 5051 | if (wasm_bits != int_info.bits) { |
| 5054 | const val: u64 = @as(u64, 1) << @intCast(u6, int_info.bits); | 5052 | const val: u64 = @as(u64, 1) << @intCast(u6, int_info.bits); |
| 5055 | // leave value on the stack | 5053 | // leave value on the stack |
| 5056 | _ = try self.binOp(operand, .{ .imm64 = val }, ty, .@"or"); | 5054 | _ = try func.binOp(operand, .{ .imm64 = val }, ty, .@"or"); |
| 5057 | } else try self.emitWValue(operand); | 5055 | } else try func.emitWValue(operand); |
| 5058 | try self.addTag(.i64_ctz); | 5056 | try func.addTag(.i64_ctz); |
| 5059 | try self.addTag(.i32_wrap_i64); | 5057 | try func.addTag(.i32_wrap_i64); |
| 5060 | }, | 5058 | }, |
| 5061 | 128 => { | 5059 | 128 => { |
| 5062 | var msb = try (try self.load(operand, Type.u64, 0)).toLocal(self, Type.u64); | 5060 | var msb = try (try func.load(operand, Type.u64, 0)).toLocal(func, Type.u64); |
| 5063 | defer msb.free(self); | 5061 | defer msb.free(func); |
| 5064 | | 5062 | |
| 5065 | try self.emitWValue(msb); | 5063 | try func.emitWValue(msb); |
| 5066 | try self.addTag(.i64_ctz); | 5064 | try func.addTag(.i64_ctz); |
| 5067 | _ = try self.load(operand, Type.u64, 8); | 5065 | _ = try func.load(operand, Type.u64, 8); |
| 5068 | if (wasm_bits != int_info.bits) { | 5066 | if (wasm_bits != int_info.bits) { |
| 5069 | try self.addImm64(@as(u64, 1) << @intCast(u6, int_info.bits - 64)); | 5067 | try func.addImm64(@as(u64, 1) << @intCast(u6, int_info.bits - 64)); |
| 5070 | try self.addTag(.i64_or); | 5068 | try func.addTag(.i64_or); |
| 5071 | } | 5069 | } |
| 5072 | try self.addTag(.i64_ctz); | 5070 | try func.addTag(.i64_ctz); |
| 5073 | try self.addImm64(64); | 5071 | try func.addImm64(64); |
| 5074 | if (wasm_bits != int_info.bits) { | 5072 | if (wasm_bits != int_info.bits) { |
| 5075 | try self.addTag(.i64_or); | 5073 | try func.addTag(.i64_or); |
| 5076 | } else { | 5074 | } else { |
| 5077 | try self.addTag(.i64_add); | 5075 | try func.addTag(.i64_add); |
| 5078 | } | 5076 | } |
| 5079 | _ = try self.cmp(msb, .{ .imm64 = 0 }, Type.u64, .neq); | 5077 | _ = try func.cmp(msb, .{ .imm64 = 0 }, Type.u64, .neq); |
| 5080 | try self.addTag(.select); | 5078 | try func.addTag(.select); |
| 5081 | try self.addTag(.i32_wrap_i64); | 5079 | try func.addTag(.i32_wrap_i64); |
| 5082 | }, | 5080 | }, |
| 5083 | else => unreachable, | 5081 | else => unreachable, |
| 5084 | } | 5082 | } |
| 5085 | | 5083 | |
| 5086 | const result = try self.allocLocal(result_ty); | 5084 | const result = try func.allocLocal(result_ty); |
| 5087 | try self.addLabel(.local_set, result.local.value); | 5085 | try func.addLabel(.local_set, result.local.value); |
| 5088 | self.finishAir(inst, result, &.{ty_op.operand}); | 5086 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5089 | } | 5087 | } |
| 5090 | | 5088 | |
| 5091 | fn airDbgVar(self: *Self, inst: Air.Inst.Index, is_ptr: bool) !void { | 5089 | fn airDbgVar(func: *CodeGen, inst: Air.Inst.Index, is_ptr: bool) !void { |
| 5092 | if (self.debug_output != .dwarf) return self.finishAir(inst, .none, &.{}); | 5090 | if (func.debug_output != .dwarf) return func.finishAir(inst, .none, &.{}); |
| 5093 | | 5091 | |
| 5094 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 5092 | const pl_op = func.air.instructions.items(.data)[inst].pl_op; |
| 5095 | const ty = self.air.typeOf(pl_op.operand); | 5093 | const ty = func.air.typeOf(pl_op.operand); |
| 5096 | const operand = try self.resolveInst(pl_op.operand); | 5094 | const operand = try func.resolveInst(pl_op.operand); |
| 5097 | const op_ty = if (is_ptr) ty.childType() else ty; | 5095 | const op_ty = if (is_ptr) ty.childType() else ty; |
| 5098 | | 5096 | |
| 5099 | log.debug("airDbgVar: %{d}: {}, {}", .{ inst, op_ty.fmtDebug(), operand }); | 5097 | log.debug("airDbgVar: %{d}: {}, {}", .{ inst, op_ty.fmtDebug(), operand }); |
| 5100 | | 5098 | |
| 5101 | const name = self.air.nullTerminatedString(pl_op.payload); | 5099 | const name = func.air.nullTerminatedString(pl_op.payload); |
| 5102 | log.debug(" var name = ({s})", .{name}); | 5100 | log.debug(" var name = ({s})", .{name}); |
| 5103 | | 5101 | |
| 5104 | const dbg_info = &self.debug_output.dwarf.dbg_info; | 5102 | const dbg_info = &func.debug_output.dwarf.dbg_info; |
| 5105 | try dbg_info.append(@enumToInt(link.File.Dwarf.AbbrevKind.variable)); | 5103 | try dbg_info.append(@enumToInt(link.File.Dwarf.AbbrevKind.variable)); |
| 5106 | switch (operand) { | 5104 | switch (operand) { |
| 5107 | .local => |local| { | 5105 | .local => |local| { |
| ... | @@ -5123,54 +5121,54 @@ fn airDbgVar(self: *Self, inst: Air.Inst.Index, is_ptr: bool) !void { | ... | @@ -5123,54 +5121,54 @@ fn airDbgVar(self: *Self, inst: Air.Inst.Index, is_ptr: bool) !void { |
| 5123 | } | 5121 | } |
| 5124 | | 5122 | |
| 5125 | try dbg_info.ensureUnusedCapacity(5 + name.len + 1); | 5123 | try dbg_info.ensureUnusedCapacity(5 + name.len + 1); |
| 5126 | try self.addDbgInfoTypeReloc(op_ty); | 5124 | try func.addDbgInfoTypeReloc(op_ty); |
| 5127 | dbg_info.appendSliceAssumeCapacity(name); | 5125 | dbg_info.appendSliceAssumeCapacity(name); |
| 5128 | dbg_info.appendAssumeCapacity(0); | 5126 | dbg_info.appendAssumeCapacity(0); |
| 5129 | self.finishAir(inst, .none, &.{}); | 5127 | func.finishAir(inst, .none, &.{}); |
| 5130 | } | 5128 | } |
| 5131 | | 5129 | |
| 5132 | fn airDbgStmt(self: *Self, inst: Air.Inst.Index) !void { | 5130 | fn airDbgStmt(func: *CodeGen, inst: Air.Inst.Index) !void { |
| 5133 | if (self.debug_output != .dwarf) return self.finishAir(inst, .none, &.{}); | 5131 | if (func.debug_output != .dwarf) return func.finishAir(inst, .none, &.{}); |
| 5134 | | 5132 | |
| 5135 | const dbg_stmt = self.air.instructions.items(.data)[inst].dbg_stmt; | 5133 | const dbg_stmt = func.air.instructions.items(.data)[inst].dbg_stmt; |
| 5136 | try self.addInst(.{ .tag = .dbg_line, .data = .{ | 5134 | try func.addInst(.{ .tag = .dbg_line, .data = .{ |
| 5137 | .payload = try self.addExtra(Mir.DbgLineColumn{ | 5135 | .payload = try func.addExtra(Mir.DbgLineColumn{ |
| 5138 | .line = dbg_stmt.line, | 5136 | .line = dbg_stmt.line, |
| 5139 | .column = dbg_stmt.column, | 5137 | .column = dbg_stmt.column, |
| 5140 | }), | 5138 | }), |
| 5141 | } }); | 5139 | } }); |
| 5142 | self.finishAir(inst, .none, &.{}); | 5140 | func.finishAir(inst, .none, &.{}); |
| 5143 | } | 5141 | } |
| 5144 | | 5142 | |
| 5145 | fn airTry(self: *Self, inst: Air.Inst.Index) InnerError!void { | 5143 | fn airTry(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5146 | const pl_op = self.air.instructions.items(.data)[inst].pl_op; | 5144 | const pl_op = func.air.instructions.items(.data)[inst].pl_op; |
| 5147 | const err_union = try self.resolveInst(pl_op.operand); | 5145 | const err_union = try func.resolveInst(pl_op.operand); |
| 5148 | const extra = self.air.extraData(Air.Try, pl_op.payload); | 5146 | const extra = func.air.extraData(Air.Try, pl_op.payload); |
| 5149 | const body = self.air.extra[extra.end..][0..extra.data.body_len]; | 5147 | const body = func.air.extra[extra.end..][0..extra.data.body_len]; |
| 5150 | const err_union_ty = self.air.typeOf(pl_op.operand); | 5148 | const err_union_ty = func.air.typeOf(pl_op.operand); |
| 5151 | const result = try lowerTry(self, err_union, body, err_union_ty, false); | 5149 | const result = try lowerTry(func, err_union, body, err_union_ty, false); |
| 5152 | self.finishAir(inst, result, &.{pl_op.operand}); | 5150 | func.finishAir(inst, result, &.{pl_op.operand}); |
| 5153 | } | 5151 | } |
| 5154 | | 5152 | |
| 5155 | fn airTryPtr(self: *Self, inst: Air.Inst.Index) InnerError!void { | 5153 | fn airTryPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5156 | const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; | 5154 | const ty_pl = func.air.instructions.items(.data)[inst].ty_pl; |
| 5157 | const extra = self.air.extraData(Air.TryPtr, ty_pl.payload); | 5155 | const extra = func.air.extraData(Air.TryPtr, ty_pl.payload); |
| 5158 | const err_union_ptr = try self.resolveInst(extra.data.ptr); | 5156 | const err_union_ptr = try func.resolveInst(extra.data.ptr); |
| 5159 | const body = self.air.extra[extra.end..][0..extra.data.body_len]; | 5157 | const body = func.air.extra[extra.end..][0..extra.data.body_len]; |
| 5160 | const err_union_ty = self.air.typeOf(extra.data.ptr).childType(); | 5158 | const err_union_ty = func.air.typeOf(extra.data.ptr).childType(); |
| 5161 | const result = try lowerTry(self, err_union_ptr, body, err_union_ty, true); | 5159 | const result = try lowerTry(func, err_union_ptr, body, err_union_ty, true); |
| 5162 | self.finishAir(inst, result, &.{extra.data.ptr}); | 5160 | func.finishAir(inst, result, &.{extra.data.ptr}); |
| 5163 | } | 5161 | } |
| 5164 | | 5162 | |
| 5165 | fn lowerTry( | 5163 | fn lowerTry( |
| 5166 | self: *Self, | 5164 | func: *CodeGen, |
| 5167 | err_union: WValue, | 5165 | err_union: WValue, |
| 5168 | body: []const Air.Inst.Index, | 5166 | body: []const Air.Inst.Index, |
| 5169 | err_union_ty: Type, | 5167 | err_union_ty: Type, |
| 5170 | operand_is_ptr: bool, | 5168 | operand_is_ptr: bool, |
| 5171 | ) InnerError!WValue { | 5169 | ) InnerError!WValue { |
| 5172 | if (operand_is_ptr) { | 5170 | if (operand_is_ptr) { |
| 5173 | return self.fail("TODO: lowerTry for pointers", .{}); | 5171 | return func.fail("TODO: lowerTry for pointers", .{}); |
| 5174 | } | 5172 | } |
| 5175 | | 5173 | |
| 5176 | const pl_ty = err_union_ty.errorUnionPayload(); | 5174 | const pl_ty = err_union_ty.errorUnionPayload(); |
| ... | @@ -5178,21 +5176,21 @@ fn lowerTry( | ... | @@ -5178,21 +5176,21 @@ fn lowerTry( |
| 5178 | | 5176 | |
| 5179 | if (!err_union_ty.errorUnionSet().errorSetIsEmpty()) { | 5177 | if (!err_union_ty.errorUnionSet().errorSetIsEmpty()) { |
| 5180 | // Block we can jump out of when error is not set | 5178 | // Block we can jump out of when error is not set |
| 5181 | try self.startBlock(.block, wasm.block_empty); | 5179 | try func.startBlock(.block, wasm.block_empty); |
| 5182 | | 5180 | |
| 5183 | // check if the error tag is set for the error union. | 5181 | // check if the error tag is set for the error union. |
| 5184 | try self.emitWValue(err_union); | 5182 | try func.emitWValue(err_union); |
| 5185 | if (pl_has_bits) { | 5183 | if (pl_has_bits) { |
| 5186 | const err_offset = @intCast(u32, errUnionErrorOffset(pl_ty, self.target)); | 5184 | const err_offset = @intCast(u32, errUnionErrorOffset(pl_ty, func.target)); |
| 5187 | try self.addMemArg(.i32_load16_u, .{ | 5185 | try func.addMemArg(.i32_load16_u, .{ |
| 5188 | .offset = err_union.offset() + err_offset, | 5186 | .offset = err_union.offset() + err_offset, |
| 5189 | .alignment = Type.anyerror.abiAlignment(self.target), | 5187 | .alignment = Type.anyerror.abiAlignment(func.target), |
| 5190 | }); | 5188 | }); |
| 5191 | } | 5189 | } |
| 5192 | try self.addTag(.i32_eqz); | 5190 | try func.addTag(.i32_eqz); |
| 5193 | try self.addLabel(.br_if, 0); // jump out of block when error is '0' | 5191 | try func.addLabel(.br_if, 0); // jump out of block when error is '0' |
| 5194 | try self.genBody(body); | 5192 | try func.genBody(body); |
| 5195 | try self.endBlock(); | 5193 | try func.endBlock(); |
| 5196 | } | 5194 | } |
| 5197 | | 5195 | |
| 5198 | // if we reach here it means error was not set, and we want the payload | 5196 | // if we reach here it means error was not set, and we want the payload |
| ... | @@ -5200,121 +5198,121 @@ fn lowerTry( | ... | @@ -5200,121 +5198,121 @@ fn lowerTry( |
| 5200 | return WValue{ .none = {} }; | 5198 | return WValue{ .none = {} }; |
| 5201 | } | 5199 | } |
| 5202 | | 5200 | |
| 5203 | const pl_offset = @intCast(u32, errUnionPayloadOffset(pl_ty, self.target)); | 5201 | const pl_offset = @intCast(u32, errUnionPayloadOffset(pl_ty, func.target)); |
| 5204 | if (isByRef(pl_ty, self.target)) { | 5202 | if (isByRef(pl_ty, func.target)) { |
| 5205 | return buildPointerOffset(self, err_union, pl_offset, .new); | 5203 | return buildPointerOffset(func, err_union, pl_offset, .new); |
| 5206 | } | 5204 | } |
| 5207 | const payload = try self.load(err_union, pl_ty, pl_offset); | 5205 | const payload = try func.load(err_union, pl_ty, pl_offset); |
| 5208 | return payload.toLocal(self, pl_ty); | 5206 | return payload.toLocal(func, pl_ty); |
| 5209 | } | 5207 | } |
| 5210 | | 5208 | |
| 5211 | fn airByteSwap(self: *Self, inst: Air.Inst.Index) InnerError!void { | 5209 | fn airByteSwap(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5212 | const ty_op = self.air.instructions.items(.data)[inst].ty_op; | 5210 | const ty_op = func.air.instructions.items(.data)[inst].ty_op; |
| 5213 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ty_op.operand}); | 5211 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ty_op.operand}); |
| 5214 | | 5212 | |
| 5215 | const ty = self.air.typeOfIndex(inst); | 5213 | const ty = func.air.typeOfIndex(inst); |
| 5216 | const operand = try self.resolveInst(ty_op.operand); | 5214 | const operand = try func.resolveInst(ty_op.operand); |
| 5217 | | 5215 | |
| 5218 | if (ty.zigTypeTag() == .Vector) { | 5216 | if (ty.zigTypeTag() == .Vector) { |
| 5219 | return self.fail("TODO: @byteSwap for vectors", .{}); | 5217 | return func.fail("TODO: @byteSwap for vectors", .{}); |
| 5220 | } | 5218 | } |
| 5221 | const int_info = ty.intInfo(self.target); | 5219 | const int_info = ty.intInfo(func.target); |
| 5222 | | 5220 | |
| 5223 | // bytes are no-op | 5221 | // bytes are no-op |
| 5224 | if (int_info.bits == 8) { | 5222 | if (int_info.bits == 8) { |
| 5225 | return self.finishAir(inst, self.reuseOperand(ty_op.operand, operand), &.{ty_op.operand}); | 5223 | return func.finishAir(inst, func.reuseOperand(ty_op.operand, operand), &.{ty_op.operand}); |
| 5226 | } | 5224 | } |
| 5227 | | 5225 | |
| 5228 | const result = result: { | 5226 | const result = result: { |
| 5229 | switch (int_info.bits) { | 5227 | switch (int_info.bits) { |
| 5230 | 16 => { | 5228 | 16 => { |
| 5231 | const shl_res = try self.binOp(operand, .{ .imm32 = 8 }, ty, .shl); | 5229 | const shl_res = try func.binOp(operand, .{ .imm32 = 8 }, ty, .shl); |
| 5232 | const lhs = try self.binOp(shl_res, .{ .imm32 = 0xFF00 }, ty, .@"and"); | 5230 | const lhs = try func.binOp(shl_res, .{ .imm32 = 0xFF00 }, ty, .@"and"); |
| 5233 | const shr_res = try self.binOp(operand, .{ .imm32 = 8 }, ty, .shr); | 5231 | const shr_res = try func.binOp(operand, .{ .imm32 = 8 }, ty, .shr); |
| 5234 | const res = if (int_info.signedness == .signed) blk: { | 5232 | const res = if (int_info.signedness == .signed) blk: { |
| 5235 | break :blk try self.wrapOperand(shr_res, Type.u8); | 5233 | break :blk try func.wrapOperand(shr_res, Type.u8); |
| 5236 | } else shr_res; | 5234 | } else shr_res; |
| 5237 | break :result try (try self.binOp(lhs, res, ty, .@"or")).toLocal(self, ty); | 5235 | break :result try (try func.binOp(lhs, res, ty, .@"or")).toLocal(func, ty); |
| 5238 | }, | 5236 | }, |
| 5239 | 24 => { | 5237 | 24 => { |
| 5240 | var msb = try (try self.wrapOperand(operand, Type.u16)).toLocal(self, Type.u16); | 5238 | var msb = try (try func.wrapOperand(operand, Type.u16)).toLocal(func, Type.u16); |
| 5241 | defer msb.free(self); | 5239 | defer msb.free(func); |
| 5242 | | 5240 | |
| 5243 | const shl_res = try self.binOp(msb, .{ .imm32 = 8 }, Type.u16, .shl); | 5241 | const shl_res = try func.binOp(msb, .{ .imm32 = 8 }, Type.u16, .shl); |
| 5244 | const lhs = try self.binOp(shl_res, .{ .imm32 = 0xFF0000 }, Type.u16, .@"and"); | 5242 | const lhs = try func.binOp(shl_res, .{ .imm32 = 0xFF0000 }, Type.u16, .@"and"); |
| 5245 | const shr_res = try self.binOp(msb, .{ .imm32 = 8 }, ty, .shr); | 5243 | const shr_res = try func.binOp(msb, .{ .imm32 = 8 }, ty, .shr); |
| 5246 | | 5244 | |
| 5247 | const res = if (int_info.signedness == .signed) blk: { | 5245 | const res = if (int_info.signedness == .signed) blk: { |
| 5248 | break :blk try self.wrapOperand(shr_res, Type.u8); | 5246 | break :blk try func.wrapOperand(shr_res, Type.u8); |
| 5249 | } else shr_res; | 5247 | } else shr_res; |
| 5250 | const lhs_tmp = try self.binOp(lhs, res, ty, .@"or"); | 5248 | const lhs_tmp = try func.binOp(lhs, res, ty, .@"or"); |
| 5251 | const lhs_result = try self.binOp(lhs_tmp, .{ .imm32 = 8 }, ty, .shr); | 5249 | const lhs_result = try func.binOp(lhs_tmp, .{ .imm32 = 8 }, ty, .shr); |
| 5252 | const rhs_wrap = try self.wrapOperand(msb, Type.u8); | 5250 | const rhs_wrap = try func.wrapOperand(msb, Type.u8); |
| 5253 | const rhs_result = try self.binOp(rhs_wrap, .{ .imm32 = 16 }, ty, .shl); | 5251 | const rhs_result = try func.binOp(rhs_wrap, .{ .imm32 = 16 }, ty, .shl); |
| 5254 | | 5252 | |
| 5255 | const lsb = try self.wrapBinOp(operand, .{ .imm32 = 16 }, Type.u8, .shr); | 5253 | const lsb = try func.wrapBinOp(operand, .{ .imm32 = 16 }, Type.u8, .shr); |
| 5256 | const tmp = try self.binOp(lhs_result, rhs_result, ty, .@"or"); | 5254 | const tmp = try func.binOp(lhs_result, rhs_result, ty, .@"or"); |
| 5257 | break :result try (try self.binOp(tmp, lsb, ty, .@"or")).toLocal(self, ty); | 5255 | break :result try (try func.binOp(tmp, lsb, ty, .@"or")).toLocal(func, ty); |
| 5258 | }, | 5256 | }, |
| 5259 | 32 => { | 5257 | 32 => { |
| 5260 | const shl_tmp = try self.binOp(operand, .{ .imm32 = 8 }, ty, .shl); | 5258 | const shl_tmp = try func.binOp(operand, .{ .imm32 = 8 }, ty, .shl); |
| 5261 | var lhs = try (try self.binOp(shl_tmp, .{ .imm32 = 0xFF00FF00 }, ty, .@"and")).toLocal(self, ty); | 5259 | var lhs = try (try func.binOp(shl_tmp, .{ .imm32 = 0xFF00FF00 }, ty, .@"and")).toLocal(func, ty); |
| 5262 | defer lhs.free(self); | 5260 | defer lhs.free(func); |
| 5263 | const shr_tmp = try self.binOp(operand, .{ .imm32 = 8 }, ty, .shr); | 5261 | const shr_tmp = try func.binOp(operand, .{ .imm32 = 8 }, ty, .shr); |
| 5264 | var rhs = try (try self.binOp(shr_tmp, .{ .imm32 = 0xFF00FF }, ty, .@"and")).toLocal(self, ty); | 5262 | var rhs = try (try func.binOp(shr_tmp, .{ .imm32 = 0xFF00FF }, ty, .@"and")).toLocal(func, ty); |
| 5265 | defer rhs.free(self); | 5263 | defer rhs.free(func); |
| 5266 | var tmp_or = try (try self.binOp(lhs, rhs, ty, .@"or")).toLocal(self, ty); | 5264 | var tmp_or = try (try func.binOp(lhs, rhs, ty, .@"or")).toLocal(func, ty); |
| 5267 | defer tmp_or.free(self); | 5265 | defer tmp_or.free(func); |
| 5268 | | 5266 | |
| 5269 | const shl = try self.binOp(tmp_or, .{ .imm32 = 16 }, ty, .shl); | 5267 | const shl = try func.binOp(tmp_or, .{ .imm32 = 16 }, ty, .shl); |
| 5270 | const shr = try self.binOp(tmp_or, .{ .imm32 = 16 }, ty, .shr); | 5268 | const shr = try func.binOp(tmp_or, .{ .imm32 = 16 }, ty, .shr); |
| 5271 | const res = if (int_info.signedness == .signed) blk: { | 5269 | const res = if (int_info.signedness == .signed) blk: { |
| 5272 | break :blk try self.wrapOperand(shr, Type.u16); | 5270 | break :blk try func.wrapOperand(shr, Type.u16); |
| 5273 | } else shr; | 5271 | } else shr; |
| 5274 | break :result try (try self.binOp(shl, res, ty, .@"or")).toLocal(self, ty); | 5272 | break :result try (try func.binOp(shl, res, ty, .@"or")).toLocal(func, ty); |
| 5275 | }, | 5273 | }, |
| 5276 | else => return self.fail("TODO: @byteSwap for integers with bitsize {d}", .{int_info.bits}), | 5274 | else => return func.fail("TODO: @byteSwap for integers with bitsize {d}", .{int_info.bits}), |
| 5277 | } | 5275 | } |
| 5278 | }; | 5276 | }; |
| 5279 | self.finishAir(inst, result, &.{ty_op.operand}); | 5277 | func.finishAir(inst, result, &.{ty_op.operand}); |
| 5280 | } | 5278 | } |
| 5281 | | 5279 | |
| 5282 | fn airDiv(self: *Self, inst: Air.Inst.Index) InnerError!void { | 5280 | fn airDiv(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5283 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 5281 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 5284 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 5282 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 5285 | | 5283 | |
| 5286 | const ty = self.air.typeOfIndex(inst); | 5284 | const ty = func.air.typeOfIndex(inst); |
| 5287 | const lhs = try self.resolveInst(bin_op.lhs); | 5285 | const lhs = try func.resolveInst(bin_op.lhs); |
| 5288 | const rhs = try self.resolveInst(bin_op.rhs); | 5286 | const rhs = try func.resolveInst(bin_op.rhs); |
| 5289 | | 5287 | |
| 5290 | const result = if (ty.isSignedInt()) | 5288 | const result = if (ty.isSignedInt()) |
| 5291 | try self.divSigned(lhs, rhs, ty) | 5289 | try func.divSigned(lhs, rhs, ty) |
| 5292 | else | 5290 | else |
| 5293 | try (try self.binOp(lhs, rhs, ty, .div)).toLocal(self, ty); | 5291 | try (try func.binOp(lhs, rhs, ty, .div)).toLocal(func, ty); |
| 5294 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 5292 | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 5295 | } | 5293 | } |
| 5296 | | 5294 | |
| 5297 | fn airDivFloor(self: *Self, inst: Air.Inst.Index) InnerError!void { | 5295 | fn airDivFloor(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5298 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 5296 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 5299 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 5297 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 5300 | | 5298 | |
| 5301 | const ty = self.air.typeOfIndex(inst); | 5299 | const ty = func.air.typeOfIndex(inst); |
| 5302 | const lhs = try self.resolveInst(bin_op.lhs); | 5300 | const lhs = try func.resolveInst(bin_op.lhs); |
| 5303 | const rhs = try self.resolveInst(bin_op.rhs); | 5301 | const rhs = try func.resolveInst(bin_op.rhs); |
| 5304 | | 5302 | |
| 5305 | if (ty.isUnsignedInt()) { | 5303 | if (ty.isUnsignedInt()) { |
| 5306 | const result = try (try self.binOp(lhs, rhs, ty, .div)).toLocal(self, ty); | 5304 | const result = try (try func.binOp(lhs, rhs, ty, .div)).toLocal(func, ty); |
| 5307 | return self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 5305 | return func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 5308 | } else if (ty.isSignedInt()) { | 5306 | } else if (ty.isSignedInt()) { |
| 5309 | const int_bits = ty.intInfo(self.target).bits; | 5307 | const int_bits = ty.intInfo(func.target).bits; |
| 5310 | const wasm_bits = toWasmBits(int_bits) orelse { | 5308 | const wasm_bits = toWasmBits(int_bits) orelse { |
| 5311 | return self.fail("TODO: `@divFloor` for signed integers larger than '{d}' bits", .{int_bits}); | 5309 | return func.fail("TODO: `@divFloor` for signed integers larger than '{d}' bits", .{int_bits}); |
| 5312 | }; | 5310 | }; |
| 5313 | const lhs_res = if (wasm_bits != int_bits) blk: { | 5311 | const lhs_res = if (wasm_bits != int_bits) blk: { |
| 5314 | break :blk try (try self.signAbsValue(lhs, ty)).toLocal(self, ty); | 5312 | break :blk try (try func.signAbsValue(lhs, ty)).toLocal(func, ty); |
| 5315 | } else lhs; | 5313 | } else lhs; |
| 5316 | const rhs_res = if (wasm_bits != int_bits) blk: { | 5314 | const rhs_res = if (wasm_bits != int_bits) blk: { |
| 5317 | break :blk try (try self.signAbsValue(rhs, ty)).toLocal(self, ty); | 5315 | break :blk try (try func.signAbsValue(rhs, ty)).toLocal(func, ty); |
| 5318 | } else rhs; | 5316 | } else rhs; |
| 5319 | | 5317 | |
| 5320 | const zero = switch (wasm_bits) { | 5318 | const zero = switch (wasm_bits) { |
| ... | @@ -5323,118 +5321,118 @@ fn airDivFloor(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -5323,118 +5321,118 @@ fn airDivFloor(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 5323 | else => unreachable, | 5321 | else => unreachable, |
| 5324 | }; | 5322 | }; |
| 5325 | | 5323 | |
| 5326 | const div_result = try self.allocLocal(ty); | 5324 | const div_result = try func.allocLocal(ty); |
| 5327 | // leave on stack | 5325 | // leave on stack |
| 5328 | _ = try self.binOp(lhs_res, rhs_res, ty, .div); | 5326 | _ = try func.binOp(lhs_res, rhs_res, ty, .div); |
| 5329 | try self.addLabel(.local_tee, div_result.local.value); | 5327 | try func.addLabel(.local_tee, div_result.local.value); |
| 5330 | _ = try self.cmp(lhs_res, zero, ty, .lt); | 5328 | _ = try func.cmp(lhs_res, zero, ty, .lt); |
| 5331 | _ = try self.cmp(rhs_res, zero, ty, .lt); | 5329 | _ = try func.cmp(rhs_res, zero, ty, .lt); |
| 5332 | switch (wasm_bits) { | 5330 | switch (wasm_bits) { |
| 5333 | 32 => { | 5331 | 32 => { |
| 5334 | try self.addTag(.i32_xor); | 5332 | try func.addTag(.i32_xor); |
| 5335 | try self.addTag(.i32_sub); | 5333 | try func.addTag(.i32_sub); |
| 5336 | }, | 5334 | }, |
| 5337 | 64 => { | 5335 | 64 => { |
| 5338 | try self.addTag(.i64_xor); | 5336 | try func.addTag(.i64_xor); |
| 5339 | try self.addTag(.i64_sub); | 5337 | try func.addTag(.i64_sub); |
| 5340 | }, | 5338 | }, |
| 5341 | else => unreachable, | 5339 | else => unreachable, |
| 5342 | } | 5340 | } |
| 5343 | try self.emitWValue(div_result); | 5341 | try func.emitWValue(div_result); |
| 5344 | // leave value on the stack | 5342 | // leave value on the stack |
| 5345 | _ = try self.binOp(lhs_res, rhs_res, ty, .rem); | 5343 | _ = try func.binOp(lhs_res, rhs_res, ty, .rem); |
| 5346 | try self.addTag(.select); | 5344 | try func.addTag(.select); |
| 5347 | } else { | 5345 | } else { |
| 5348 | const float_bits = ty.floatBits(self.target); | 5346 | const float_bits = ty.floatBits(func.target); |
| 5349 | if (float_bits > 64) { | 5347 | if (float_bits > 64) { |
| 5350 | return self.fail("TODO: `@divFloor` for floats with bitsize: {d}", .{float_bits}); | 5348 | return func.fail("TODO: `@divFloor` for floats with bitsize: {d}", .{float_bits}); |
| 5351 | } | 5349 | } |
| 5352 | const is_f16 = float_bits == 16; | 5350 | const is_f16 = float_bits == 16; |
| 5353 | | 5351 | |
| 5354 | const lhs_operand = if (is_f16) blk: { | 5352 | const lhs_operand = if (is_f16) blk: { |
| 5355 | break :blk try self.fpext(lhs, Type.f16, Type.f32); | 5353 | break :blk try func.fpext(lhs, Type.f16, Type.f32); |
| 5356 | } else lhs; | 5354 | } else lhs; |
| 5357 | const rhs_operand = if (is_f16) blk: { | 5355 | const rhs_operand = if (is_f16) blk: { |
| 5358 | break :blk try self.fpext(rhs, Type.f16, Type.f32); | 5356 | break :blk try func.fpext(rhs, Type.f16, Type.f32); |
| 5359 | } else rhs; | 5357 | } else rhs; |
| 5360 | | 5358 | |
| 5361 | try self.emitWValue(lhs_operand); | 5359 | try func.emitWValue(lhs_operand); |
| 5362 | try self.emitWValue(rhs_operand); | 5360 | try func.emitWValue(rhs_operand); |
| 5363 | | 5361 | |
| 5364 | switch (float_bits) { | 5362 | switch (float_bits) { |
| 5365 | 16, 32 => { | 5363 | 16, 32 => { |
| 5366 | try self.addTag(.f32_div); | 5364 | try func.addTag(.f32_div); |
| 5367 | try self.addTag(.f32_floor); | 5365 | try func.addTag(.f32_floor); |
| 5368 | }, | 5366 | }, |
| 5369 | 64 => { | 5367 | 64 => { |
| 5370 | try self.addTag(.f64_div); | 5368 | try func.addTag(.f64_div); |
| 5371 | try self.addTag(.f64_floor); | 5369 | try func.addTag(.f64_floor); |
| 5372 | }, | 5370 | }, |
| 5373 | else => unreachable, | 5371 | else => unreachable, |
| 5374 | } | 5372 | } |
| 5375 | | 5373 | |
| 5376 | if (is_f16) { | 5374 | if (is_f16) { |
| 5377 | _ = try self.fptrunc(.{ .stack = {} }, Type.f32, Type.f16); | 5375 | _ = try func.fptrunc(.{ .stack = {} }, Type.f32, Type.f16); |
| 5378 | } | 5376 | } |
| 5379 | } | 5377 | } |
| 5380 | | 5378 | |
| 5381 | const result = try self.allocLocal(ty); | 5379 | const result = try func.allocLocal(ty); |
| 5382 | try self.addLabel(.local_set, result.local.value); | 5380 | try func.addLabel(.local_set, result.local.value); |
| 5383 | self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 5381 | func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 5384 | } | 5382 | } |
| 5385 | | 5383 | |
| 5386 | fn divSigned(self: *Self, lhs: WValue, rhs: WValue, ty: Type) InnerError!WValue { | 5384 | fn divSigned(func: *CodeGen, lhs: WValue, rhs: WValue, ty: Type) InnerError!WValue { |
| 5387 | const int_bits = ty.intInfo(self.target).bits; | 5385 | const int_bits = ty.intInfo(func.target).bits; |
| 5388 | const wasm_bits = toWasmBits(int_bits) orelse { | 5386 | const wasm_bits = toWasmBits(int_bits) orelse { |
| 5389 | return self.fail("TODO: Implement signed division for integers with bitsize '{d}'", .{int_bits}); | 5387 | return func.fail("TODO: Implement signed division for integers with bitsize '{d}'", .{int_bits}); |
| 5390 | }; | 5388 | }; |
| 5391 | | 5389 | |
| 5392 | if (wasm_bits == 128) { | 5390 | if (wasm_bits == 128) { |
| 5393 | return self.fail("TODO: Implement signed division for 128-bit integerrs", .{}); | 5391 | return func.fail("TODO: Implement signed division for 128-bit integerrs", .{}); |
| 5394 | } | 5392 | } |
| 5395 | | 5393 | |
| 5396 | if (wasm_bits != int_bits) { | 5394 | if (wasm_bits != int_bits) { |
| 5397 | // Leave both values on the stack | 5395 | // Leave both values on the stack |
| 5398 | _ = try self.signAbsValue(lhs, ty); | 5396 | _ = try func.signAbsValue(lhs, ty); |
| 5399 | _ = try self.signAbsValue(rhs, ty); | 5397 | _ = try func.signAbsValue(rhs, ty); |
| 5400 | } else { | 5398 | } else { |
| 5401 | try self.emitWValue(lhs); | 5399 | try func.emitWValue(lhs); |
| 5402 | try self.emitWValue(rhs); | 5400 | try func.emitWValue(rhs); |
| 5403 | } | 5401 | } |
| 5404 | try self.addTag(.i32_div_s); | 5402 | try func.addTag(.i32_div_s); |
| 5405 | | 5403 | |
| 5406 | const result = try self.allocLocal(ty); | 5404 | const result = try func.allocLocal(ty); |
| 5407 | try self.addLabel(.local_set, result.local.value); | 5405 | try func.addLabel(.local_set, result.local.value); |
| 5408 | return result; | 5406 | return result; |
| 5409 | } | 5407 | } |
| 5410 | | 5408 | |
| 5411 | /// Retrieves the absolute value of a signed integer | 5409 | /// Retrieves the absolute value of a signed integer |
| 5412 | /// NOTE: Leaves the result value on the stack. | 5410 | /// NOTE: Leaves the result value on the stack. |
| 5413 | fn signAbsValue(self: *Self, operand: WValue, ty: Type) InnerError!WValue { | 5411 | fn signAbsValue(func: *CodeGen, operand: WValue, ty: Type) InnerError!WValue { |
| 5414 | const int_bits = ty.intInfo(self.target).bits; | 5412 | const int_bits = ty.intInfo(func.target).bits; |
| 5415 | const wasm_bits = toWasmBits(int_bits) orelse { | 5413 | const wasm_bits = toWasmBits(int_bits) orelse { |
| 5416 | return self.fail("TODO: signAbsValue for signed integers larger than '{d}' bits", .{int_bits}); | 5414 | return func.fail("TODO: signAbsValue for signed integers larger than '{d}' bits", .{int_bits}); |
| 5417 | }; | 5415 | }; |
| 5418 | | 5416 | |
| 5419 | const shift_val = switch (wasm_bits) { | 5417 | const shift_val = switch (wasm_bits) { |
| 5420 | 32 => WValue{ .imm32 = wasm_bits - int_bits }, | 5418 | 32 => WValue{ .imm32 = wasm_bits - int_bits }, |
| 5421 | 64 => WValue{ .imm64 = wasm_bits - int_bits }, | 5419 | 64 => WValue{ .imm64 = wasm_bits - int_bits }, |
| 5422 | else => return self.fail("TODO: signAbsValue for i128", .{}), | 5420 | else => return func.fail("TODO: signAbsValue for i128", .{}), |
| 5423 | }; | 5421 | }; |
| 5424 | | 5422 | |
| 5425 | try self.emitWValue(operand); | 5423 | try func.emitWValue(operand); |
| 5426 | switch (wasm_bits) { | 5424 | switch (wasm_bits) { |
| 5427 | 32 => { | 5425 | 32 => { |
| 5428 | try self.emitWValue(shift_val); | 5426 | try func.emitWValue(shift_val); |
| 5429 | try self.addTag(.i32_shl); | 5427 | try func.addTag(.i32_shl); |
| 5430 | try self.emitWValue(shift_val); | 5428 | try func.emitWValue(shift_val); |
| 5431 | try self.addTag(.i32_shr_s); | 5429 | try func.addTag(.i32_shr_s); |
| 5432 | }, | 5430 | }, |
| 5433 | 64 => { | 5431 | 64 => { |
| 5434 | try self.emitWValue(shift_val); | 5432 | try func.emitWValue(shift_val); |
| 5435 | try self.addTag(.i64_shl); | 5433 | try func.addTag(.i64_shl); |
| 5436 | try self.emitWValue(shift_val); | 5434 | try func.emitWValue(shift_val); |
| 5437 | try self.addTag(.i64_shr_s); | 5435 | try func.addTag(.i64_shr_s); |
| 5438 | }, | 5436 | }, |
| 5439 | else => unreachable, | 5437 | else => unreachable, |
| 5440 | } | 5438 | } |
| ... | @@ -5442,62 +5440,62 @@ fn signAbsValue(self: *Self, operand: WValue, ty: Type) InnerError!WValue { | ... | @@ -5442,62 +5440,62 @@ fn signAbsValue(self: *Self, operand: WValue, ty: Type) InnerError!WValue { |
| 5442 | return WValue{ .stack = {} }; | 5440 | return WValue{ .stack = {} }; |
| 5443 | } | 5441 | } |
| 5444 | | 5442 | |
| 5445 | fn airCeilFloorTrunc(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { | 5443 | fn airCeilFloorTrunc(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 5446 | const un_op = self.air.instructions.items(.data)[inst].un_op; | 5444 | const un_op = func.air.instructions.items(.data)[inst].un_op; |
| 5447 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{un_op}); | 5445 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{un_op}); |
| 5448 | | 5446 | |
| 5449 | const ty = self.air.typeOfIndex(inst); | 5447 | const ty = func.air.typeOfIndex(inst); |
| 5450 | const float_bits = ty.floatBits(self.target); | 5448 | const float_bits = ty.floatBits(func.target); |
| 5451 | const is_f16 = float_bits == 16; | 5449 | const is_f16 = float_bits == 16; |
| 5452 | | 5450 | |
| 5453 | if (ty.zigTypeTag() == .Vector) { | 5451 | if (ty.zigTypeTag() == .Vector) { |
| 5454 | return self.fail("TODO: Implement `@ceil` for vectors", .{}); | 5452 | return func.fail("TODO: Implement `@ceil` for vectors", .{}); |
| 5455 | } | 5453 | } |
| 5456 | if (float_bits > 64) { | 5454 | if (float_bits > 64) { |
| 5457 | return self.fail("TODO: implement `@ceil`, `@trunc`, `@floor` for floats larger than 64bits", .{}); | 5455 | return func.fail("TODO: implement `@ceil`, `@trunc`, `@floor` for floats larger than 64bits", .{}); |
| 5458 | } | 5456 | } |
| 5459 | | 5457 | |
| 5460 | const operand = try self.resolveInst(un_op); | 5458 | const operand = try func.resolveInst(un_op); |
| 5461 | const op_to_lower = if (is_f16) blk: { | 5459 | const op_to_lower = if (is_f16) blk: { |
| 5462 | break :blk try self.fpext(operand, Type.f16, Type.f32); | 5460 | break :blk try func.fpext(operand, Type.f16, Type.f32); |
| 5463 | } else operand; | 5461 | } else operand; |
| 5464 | try self.emitWValue(op_to_lower); | 5462 | try func.emitWValue(op_to_lower); |
| 5465 | const opcode = buildOpcode(.{ .op = op, .valtype1 = typeToValtype(ty, self.target) }); | 5463 | const opcode = buildOpcode(.{ .op = op, .valtype1 = typeToValtype(ty, func.target) }); |
| 5466 | try self.addTag(Mir.Inst.Tag.fromOpcode(opcode)); | 5464 | try func.addTag(Mir.Inst.Tag.fromOpcode(opcode)); |
| 5467 | | 5465 | |
| 5468 | if (is_f16) { | 5466 | if (is_f16) { |
| 5469 | _ = try self.fptrunc(.{ .stack = {} }, Type.f32, Type.f16); | 5467 | _ = try func.fptrunc(.{ .stack = {} }, Type.f32, Type.f16); |
| 5470 | } | 5468 | } |
| 5471 | | 5469 | |
| 5472 | const result = try self.allocLocal(ty); | 5470 | const result = try func.allocLocal(ty); |
| 5473 | try self.addLabel(.local_set, result.local.value); | 5471 | try func.addLabel(.local_set, result.local.value); |
| 5474 | self.finishAir(inst, result, &.{un_op}); | 5472 | func.finishAir(inst, result, &.{un_op}); |
| 5475 | } | 5473 | } |
| 5476 | | 5474 | |
| 5477 | fn airSatBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { | 5475 | fn airSatBinOp(func: *CodeGen, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 5478 | assert(op == .add or op == .sub); | 5476 | assert(op == .add or op == .sub); |
| 5479 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 5477 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 5480 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 5478 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 5481 | | 5479 | |
| 5482 | const ty = self.air.typeOfIndex(inst); | 5480 | const ty = func.air.typeOfIndex(inst); |
| 5483 | const lhs = try self.resolveInst(bin_op.lhs); | 5481 | const lhs = try func.resolveInst(bin_op.lhs); |
| 5484 | const rhs = try self.resolveInst(bin_op.rhs); | 5482 | const rhs = try func.resolveInst(bin_op.rhs); |
| 5485 | | 5483 | |
| 5486 | const int_info = ty.intInfo(self.target); | 5484 | const int_info = ty.intInfo(func.target); |
| 5487 | const is_signed = int_info.signedness == .signed; | 5485 | const is_signed = int_info.signedness == .signed; |
| 5488 | | 5486 | |
| 5489 | if (int_info.bits > 64) { | 5487 | if (int_info.bits > 64) { |
| 5490 | return self.fail("TODO: saturating arithmetic for integers with bitsize '{d}'", .{int_info.bits}); | 5488 | return func.fail("TODO: saturating arithmetic for integers with bitsize '{d}'", .{int_info.bits}); |
| 5491 | } | 5489 | } |
| 5492 | | 5490 | |
| 5493 | if (is_signed) { | 5491 | if (is_signed) { |
| 5494 | const result = try signedSat(self, lhs, rhs, ty, op); | 5492 | const result = try signedSat(func, lhs, rhs, ty, op); |
| 5495 | return self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 5493 | return func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 5496 | } | 5494 | } |
| 5497 | | 5495 | |
| 5498 | const wasm_bits = toWasmBits(int_info.bits).?; | 5496 | const wasm_bits = toWasmBits(int_info.bits).?; |
| 5499 | var bin_result = try (try self.binOp(lhs, rhs, ty, op)).toLocal(self, ty); | 5497 | var bin_result = try (try func.binOp(lhs, rhs, ty, op)).toLocal(func, ty); |
| 5500 | defer bin_result.free(self); | 5498 | defer bin_result.free(func); |
| 5501 | if (wasm_bits != int_info.bits and op == .add) { | 5499 | if (wasm_bits != int_info.bits and op == .add) { |
| 5502 | const val: u64 = @intCast(u64, (@as(u65, 1) << @intCast(u7, int_info.bits)) - 1); | 5500 | const val: u64 = @intCast(u64, (@as(u65, 1) << @intCast(u7, int_info.bits)) - 1); |
| 5503 | const imm_val = switch (wasm_bits) { | 5501 | const imm_val = switch (wasm_bits) { |
| ... | @@ -5506,35 +5504,35 @@ fn airSatBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { | ... | @@ -5506,35 +5504,35 @@ fn airSatBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!void { |
| 5506 | else => unreachable, | 5504 | else => unreachable, |
| 5507 | }; | 5505 | }; |
| 5508 | | 5506 | |
| 5509 | try self.emitWValue(bin_result); | 5507 | try func.emitWValue(bin_result); |
| 5510 | try self.emitWValue(imm_val); | 5508 | try func.emitWValue(imm_val); |
| 5511 | _ = try self.cmp(bin_result, imm_val, ty, .lt); | 5509 | _ = try func.cmp(bin_result, imm_val, ty, .lt); |
| 5512 | } else { | 5510 | } else { |
| 5513 | switch (wasm_bits) { | 5511 | switch (wasm_bits) { |
| 5514 | 32 => try self.addImm32(if (op == .add) @as(i32, -1) else 0), | 5512 | 32 => try func.addImm32(if (op == .add) @as(i32, -1) else 0), |
| 5515 | 64 => try self.addImm64(if (op == .add) @bitCast(u64, @as(i64, -1)) else 0), | 5513 | 64 => try func.addImm64(if (op == .add) @bitCast(u64, @as(i64, -1)) else 0), |
| 5516 | else => unreachable, | 5514 | else => unreachable, |
| 5517 | } | 5515 | } |
| 5518 | try self.emitWValue(bin_result); | 5516 | try func.emitWValue(bin_result); |
| 5519 | _ = try self.cmp(bin_result, lhs, ty, if (op == .add) .lt else .gt); | 5517 | _ = try func.cmp(bin_result, lhs, ty, if (op == .add) .lt else .gt); |
| 5520 | } | 5518 | } |
| 5521 | | 5519 | |
| 5522 | try self.addTag(.select); | 5520 | try func.addTag(.select); |
| 5523 | const result = try self.allocLocal(ty); | 5521 | const result = try func.allocLocal(ty); |
| 5524 | try self.addLabel(.local_set, result.local.value); | 5522 | try func.addLabel(.local_set, result.local.value); |
| 5525 | return self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 5523 | return func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 5526 | } | 5524 | } |
| 5527 | | 5525 | |
| 5528 | fn signedSat(self: *Self, lhs_operand: WValue, rhs_operand: WValue, ty: Type, op: Op) InnerError!WValue { | 5526 | fn signedSat(func: *CodeGen, lhs_operand: WValue, rhs_operand: WValue, ty: Type, op: Op) InnerError!WValue { |
| 5529 | const int_info = ty.intInfo(self.target); | 5527 | const int_info = ty.intInfo(func.target); |
| 5530 | const wasm_bits = toWasmBits(int_info.bits).?; | 5528 | const wasm_bits = toWasmBits(int_info.bits).?; |
| 5531 | const is_wasm_bits = wasm_bits == int_info.bits; | 5529 | const is_wasm_bits = wasm_bits == int_info.bits; |
| 5532 | | 5530 | |
| 5533 | var lhs = if (!is_wasm_bits) lhs: { | 5531 | var lhs = if (!is_wasm_bits) lhs: { |
| 5534 | break :lhs try (try self.signAbsValue(lhs_operand, ty)).toLocal(self, ty); | 5532 | break :lhs try (try func.signAbsValue(lhs_operand, ty)).toLocal(func, ty); |
| 5535 | } else lhs_operand; | 5533 | } else lhs_operand; |
| 5536 | var rhs = if (!is_wasm_bits) rhs: { | 5534 | var rhs = if (!is_wasm_bits) rhs: { |
| 5537 | break :rhs try (try self.signAbsValue(rhs_operand, ty)).toLocal(self, ty); | 5535 | break :rhs try (try func.signAbsValue(rhs_operand, ty)).toLocal(func, ty); |
| 5538 | } else rhs_operand; | 5536 | } else rhs_operand; |
| 5539 | | 5537 | |
| 5540 | const max_val: u64 = @intCast(u64, (@as(u65, 1) << @intCast(u7, int_info.bits - 1)) - 1); | 5538 | const max_val: u64 = @intCast(u64, (@as(u65, 1) << @intCast(u7, int_info.bits - 1)) - 1); |
| ... | @@ -5550,93 +5548,93 @@ fn signedSat(self: *Self, lhs_operand: WValue, rhs_operand: WValue, ty: Type, op | ... | @@ -5550,93 +5548,93 @@ fn signedSat(self: *Self, lhs_operand: WValue, rhs_operand: WValue, ty: Type, op |
| 5550 | else => unreachable, | 5548 | else => unreachable, |
| 5551 | }; | 5549 | }; |
| 5552 | | 5550 | |
| 5553 | var bin_result = try (try self.binOp(lhs, rhs, ty, op)).toLocal(self, ty); | 5551 | var bin_result = try (try func.binOp(lhs, rhs, ty, op)).toLocal(func, ty); |
| 5554 | if (!is_wasm_bits) { | 5552 | if (!is_wasm_bits) { |
| 5555 | defer bin_result.free(self); // not returned in this branch | 5553 | defer bin_result.free(func); // not returned in this branch |
| 5556 | defer lhs.free(self); // uses temporary local for absvalue | 5554 | defer lhs.free(func); // uses temporary local for absvalue |
| 5557 | defer rhs.free(self); // uses temporary local for absvalue | 5555 | defer rhs.free(func); // uses temporary local for absvalue |
| 5558 | try self.emitWValue(bin_result); | 5556 | try func.emitWValue(bin_result); |
| 5559 | try self.emitWValue(max_wvalue); | 5557 | try func.emitWValue(max_wvalue); |
| 5560 | _ = try self.cmp(bin_result, max_wvalue, ty, .lt); | 5558 | _ = try func.cmp(bin_result, max_wvalue, ty, .lt); |
| 5561 | try self.addTag(.select); | 5559 | try func.addTag(.select); |
| 5562 | try self.addLabel(.local_set, bin_result.local.value); // re-use local | 5560 | try func.addLabel(.local_set, bin_result.local.value); // re-use local |
| 5563 | | 5561 | |
| 5564 | try self.emitWValue(bin_result); | 5562 | try func.emitWValue(bin_result); |
| 5565 | try self.emitWValue(min_wvalue); | 5563 | try func.emitWValue(min_wvalue); |
| 5566 | _ = try self.cmp(bin_result, min_wvalue, ty, .gt); | 5564 | _ = try func.cmp(bin_result, min_wvalue, ty, .gt); |
| 5567 | try self.addTag(.select); | 5565 | try func.addTag(.select); |
| 5568 | try self.addLabel(.local_set, bin_result.local.value); // re-use local | 5566 | try func.addLabel(.local_set, bin_result.local.value); // re-use local |
| 5569 | return (try self.wrapOperand(bin_result, ty)).toLocal(self, ty); | 5567 | return (try func.wrapOperand(bin_result, ty)).toLocal(func, ty); |
| 5570 | } else { | 5568 | } else { |
| 5571 | const zero = switch (wasm_bits) { | 5569 | const zero = switch (wasm_bits) { |
| 5572 | 32 => WValue{ .imm32 = 0 }, | 5570 | 32 => WValue{ .imm32 = 0 }, |
| 5573 | 64 => WValue{ .imm64 = 0 }, | 5571 | 64 => WValue{ .imm64 = 0 }, |
| 5574 | else => unreachable, | 5572 | else => unreachable, |
| 5575 | }; | 5573 | }; |
| 5576 | try self.emitWValue(max_wvalue); | 5574 | try func.emitWValue(max_wvalue); |
| 5577 | try self.emitWValue(min_wvalue); | 5575 | try func.emitWValue(min_wvalue); |
| 5578 | _ = try self.cmp(bin_result, zero, ty, .lt); | 5576 | _ = try func.cmp(bin_result, zero, ty, .lt); |
| 5579 | try self.addTag(.select); | 5577 | try func.addTag(.select); |
| 5580 | try self.emitWValue(bin_result); | 5578 | try func.emitWValue(bin_result); |
| 5581 | // leave on stack | 5579 | // leave on stack |
| 5582 | const cmp_zero_result = try self.cmp(rhs, zero, ty, if (op == .add) .lt else .gt); | 5580 | const cmp_zero_result = try func.cmp(rhs, zero, ty, if (op == .add) .lt else .gt); |
| 5583 | const cmp_bin_result = try self.cmp(bin_result, lhs, ty, .lt); | 5581 | const cmp_bin_result = try func.cmp(bin_result, lhs, ty, .lt); |
| 5584 | _ = try self.binOp(cmp_zero_result, cmp_bin_result, Type.u32, .xor); // comparisons always return i32, so provide u32 as type to xor. | 5582 | _ = try func.binOp(cmp_zero_result, cmp_bin_result, Type.u32, .xor); // comparisons always return i32, so provide u32 as type to xor. |
| 5585 | try self.addTag(.select); | 5583 | try func.addTag(.select); |
| 5586 | try self.addLabel(.local_set, bin_result.local.value); // re-use local | 5584 | try func.addLabel(.local_set, bin_result.local.value); // re-use local |
| 5587 | return bin_result; | 5585 | return bin_result; |
| 5588 | } | 5586 | } |
| 5589 | } | 5587 | } |
| 5590 | | 5588 | |
| 5591 | fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!void { | 5589 | fn airShlSat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void { |
| 5592 | const bin_op = self.air.instructions.items(.data)[inst].bin_op; | 5590 | const bin_op = func.air.instructions.items(.data)[inst].bin_op; |
| 5593 | if (self.liveness.isUnused(inst)) return self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); | 5591 | if (func.liveness.isUnused(inst)) return func.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs }); |
| 5594 | | 5592 | |
| 5595 | const ty = self.air.typeOfIndex(inst); | 5593 | const ty = func.air.typeOfIndex(inst); |
| 5596 | const int_info = ty.intInfo(self.target); | 5594 | const int_info = ty.intInfo(func.target); |
| 5597 | const is_signed = int_info.signedness == .signed; | 5595 | const is_signed = int_info.signedness == .signed; |
| 5598 | if (int_info.bits > 64) { | 5596 | if (int_info.bits > 64) { |
| 5599 | return self.fail("TODO: Saturating shifting left for integers with bitsize '{d}'", .{int_info.bits}); | 5597 | return func.fail("TODO: Saturating shifting left for integers with bitsize '{d}'", .{int_info.bits}); |
| 5600 | } | 5598 | } |
| 5601 | | 5599 | |
| 5602 | const lhs = try self.resolveInst(bin_op.lhs); | 5600 | const lhs = try func.resolveInst(bin_op.lhs); |
| 5603 | const rhs = try self.resolveInst(bin_op.rhs); | 5601 | const rhs = try func.resolveInst(bin_op.rhs); |
| 5604 | const wasm_bits = toWasmBits(int_info.bits).?; | 5602 | const wasm_bits = toWasmBits(int_info.bits).?; |
| 5605 | const result = try self.allocLocal(ty); | 5603 | const result = try func.allocLocal(ty); |
| 5606 | | 5604 | |
| 5607 | if (wasm_bits == int_info.bits) outer_blk: { | 5605 | if (wasm_bits == int_info.bits) outer_blk: { |
| 5608 | var shl = try (try self.binOp(lhs, rhs, ty, .shl)).toLocal(self, ty); | 5606 | var shl = try (try func.binOp(lhs, rhs, ty, .shl)).toLocal(func, ty); |
| 5609 | defer shl.free(self); | 5607 | defer shl.free(func); |
| 5610 | var shr = try (try self.binOp(shl, rhs, ty, .shr)).toLocal(self, ty); | 5608 | var shr = try (try func.binOp(shl, rhs, ty, .shr)).toLocal(func, ty); |
| 5611 | defer shr.free(self); | 5609 | defer shr.free(func); |
| 5612 | | 5610 | |
| 5613 | switch (wasm_bits) { | 5611 | switch (wasm_bits) { |
| 5614 | 32 => blk: { | 5612 | 32 => blk: { |
| 5615 | if (!is_signed) { | 5613 | if (!is_signed) { |
| 5616 | try self.addImm32(-1); | 5614 | try func.addImm32(-1); |
| 5617 | break :blk; | 5615 | break :blk; |
| 5618 | } | 5616 | } |
| 5619 | try self.addImm32(std.math.minInt(i32)); | 5617 | try func.addImm32(std.math.minInt(i32)); |
| 5620 | try self.addImm32(std.math.maxInt(i32)); | 5618 | try func.addImm32(std.math.maxInt(i32)); |
| 5621 | _ = try self.cmp(lhs, .{ .imm32 = 0 }, ty, .lt); | 5619 | _ = try func.cmp(lhs, .{ .imm32 = 0 }, ty, .lt); |
| 5622 | try self.addTag(.select); | 5620 | try func.addTag(.select); |
| 5623 | }, | 5621 | }, |
| 5624 | 64 => blk: { | 5622 | 64 => blk: { |
| 5625 | if (!is_signed) { | 5623 | if (!is_signed) { |
| 5626 | try self.addImm64(@bitCast(u64, @as(i64, -1))); | 5624 | try func.addImm64(@bitCast(u64, @as(i64, -1))); |
| 5627 | break :blk; | 5625 | break :blk; |
| 5628 | } | 5626 | } |
| 5629 | try self.addImm64(@bitCast(u64, @as(i64, std.math.minInt(i64)))); | 5627 | try func.addImm64(@bitCast(u64, @as(i64, std.math.minInt(i64)))); |
| 5630 | try self.addImm64(@bitCast(u64, @as(i64, std.math.maxInt(i64)))); | 5628 | try func.addImm64(@bitCast(u64, @as(i64, std.math.maxInt(i64)))); |
| 5631 | _ = try self.cmp(lhs, .{ .imm64 = 0 }, ty, .lt); | 5629 | _ = try func.cmp(lhs, .{ .imm64 = 0 }, ty, .lt); |
| 5632 | try self.addTag(.select); | 5630 | try func.addTag(.select); |
| 5633 | }, | 5631 | }, |
| 5634 | else => unreachable, | 5632 | else => unreachable, |
| 5635 | } | 5633 | } |
| 5636 | try self.emitWValue(shl); | 5634 | try func.emitWValue(shl); |
| 5637 | _ = try self.cmp(lhs, shr, ty, .neq); | 5635 | _ = try func.cmp(lhs, shr, ty, .neq); |
| 5638 | try self.addTag(.select); | 5636 | try func.addTag(.select); |
| 5639 | try self.addLabel(.local_set, result.local.value); | 5637 | try func.addLabel(.local_set, result.local.value); |
| 5640 | break :outer_blk; | 5638 | break :outer_blk; |
| 5641 | } else { | 5639 | } else { |
| 5642 | const shift_size = wasm_bits - int_info.bits; | 5640 | const shift_size = wasm_bits - int_info.bits; |
| ... | @@ -5646,50 +5644,50 @@ fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -5646,50 +5644,50 @@ fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 5646 | else => unreachable, | 5644 | else => unreachable, |
| 5647 | }; | 5645 | }; |
| 5648 | | 5646 | |
| 5649 | var shl_res = try (try self.binOp(lhs, shift_value, ty, .shl)).toLocal(self, ty); | 5647 | var shl_res = try (try func.binOp(lhs, shift_value, ty, .shl)).toLocal(func, ty); |
| 5650 | defer shl_res.free(self); | 5648 | defer shl_res.free(func); |
| 5651 | var shl = try (try self.binOp(shl_res, rhs, ty, .shl)).toLocal(self, ty); | 5649 | var shl = try (try func.binOp(shl_res, rhs, ty, .shl)).toLocal(func, ty); |
| 5652 | defer shl.free(self); | 5650 | defer shl.free(func); |
| 5653 | var shr = try (try self.binOp(shl, rhs, ty, .shr)).toLocal(self, ty); | 5651 | var shr = try (try func.binOp(shl, rhs, ty, .shr)).toLocal(func, ty); |
| 5654 | defer shr.free(self); | 5652 | defer shr.free(func); |
| 5655 | | 5653 | |
| 5656 | switch (wasm_bits) { | 5654 | switch (wasm_bits) { |
| 5657 | 32 => blk: { | 5655 | 32 => blk: { |
| 5658 | if (!is_signed) { | 5656 | if (!is_signed) { |
| 5659 | try self.addImm32(-1); | 5657 | try func.addImm32(-1); |
| 5660 | break :blk; | 5658 | break :blk; |
| 5661 | } | 5659 | } |
| 5662 | | 5660 | |
| 5663 | try self.addImm32(std.math.minInt(i32)); | 5661 | try func.addImm32(std.math.minInt(i32)); |
| 5664 | try self.addImm32(std.math.maxInt(i32)); | 5662 | try func.addImm32(std.math.maxInt(i32)); |
| 5665 | _ = try self.cmp(shl_res, .{ .imm32 = 0 }, ty, .lt); | 5663 | _ = try func.cmp(shl_res, .{ .imm32 = 0 }, ty, .lt); |
| 5666 | try self.addTag(.select); | 5664 | try func.addTag(.select); |
| 5667 | }, | 5665 | }, |
| 5668 | 64 => blk: { | 5666 | 64 => blk: { |
| 5669 | if (!is_signed) { | 5667 | if (!is_signed) { |
| 5670 | try self.addImm64(@bitCast(u64, @as(i64, -1))); | 5668 | try func.addImm64(@bitCast(u64, @as(i64, -1))); |
| 5671 | break :blk; | 5669 | break :blk; |
| 5672 | } | 5670 | } |
| 5673 | | 5671 | |
| 5674 | try self.addImm64(@bitCast(u64, @as(i64, std.math.minInt(i64)))); | 5672 | try func.addImm64(@bitCast(u64, @as(i64, std.math.minInt(i64)))); |
| 5675 | try self.addImm64(@bitCast(u64, @as(i64, std.math.maxInt(i64)))); | 5673 | try func.addImm64(@bitCast(u64, @as(i64, std.math.maxInt(i64)))); |
| 5676 | _ = try self.cmp(shl_res, .{ .imm64 = 0 }, ty, .lt); | 5674 | _ = try func.cmp(shl_res, .{ .imm64 = 0 }, ty, .lt); |
| 5677 | try self.addTag(.select); | 5675 | try func.addTag(.select); |
| 5678 | }, | 5676 | }, |
| 5679 | else => unreachable, | 5677 | else => unreachable, |
| 5680 | } | 5678 | } |
| 5681 | try self.emitWValue(shl); | 5679 | try func.emitWValue(shl); |
| 5682 | _ = try self.cmp(shl_res, shr, ty, .neq); | 5680 | _ = try func.cmp(shl_res, shr, ty, .neq); |
| 5683 | try self.addTag(.select); | 5681 | try func.addTag(.select); |
| 5684 | try self.addLabel(.local_set, result.local.value); | 5682 | try func.addLabel(.local_set, result.local.value); |
| 5685 | var shift_result = try self.binOp(result, shift_value, ty, .shr); | 5683 | var shift_result = try func.binOp(result, shift_value, ty, .shr); |
| 5686 | if (is_signed) { | 5684 | if (is_signed) { |
| 5687 | shift_result = try self.wrapOperand(shift_result, ty); | 5685 | shift_result = try func.wrapOperand(shift_result, ty); |
| 5688 | } | 5686 | } |
| 5689 | try self.addLabel(.local_set, result.local.value); | 5687 | try func.addLabel(.local_set, result.local.value); |
| 5690 | } | 5688 | } |
| 5691 | | 5689 | |
| 5692 | return self.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); | 5690 | return func.finishAir(inst, result, &.{ bin_op.lhs, bin_op.rhs }); |
| 5693 | } | 5691 | } |
| 5694 | | 5692 | |
| 5695 | /// Calls a compiler-rt intrinsic by creating an undefined symbol, | 5693 | /// Calls a compiler-rt intrinsic by creating an undefined symbol, |
| ... | @@ -5699,29 +5697,29 @@ fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!void { | ... | @@ -5699,29 +5697,29 @@ fn airShlSat(self: *Self, inst: Air.Inst.Index) InnerError!void { |
| 5699 | /// passed as the first parameter. | 5697 | /// passed as the first parameter. |
| 5700 | /// May leave the return value on the stack. | 5698 | /// May leave the return value on the stack. |
| 5701 | fn callIntrinsic( | 5699 | fn callIntrinsic( |
| 5702 | self: *Self, | 5700 | func: *CodeGen, |
| 5703 | name: []const u8, | 5701 | name: []const u8, |
| 5704 | param_types: []const Type, | 5702 | param_types: []const Type, |
| 5705 | return_type: Type, | 5703 | return_type: Type, |
| 5706 | args: []const WValue, | 5704 | args: []const WValue, |
| 5707 | ) InnerError!WValue { | 5705 | ) InnerError!WValue { |
| 5708 | assert(param_types.len == args.len); | 5706 | assert(param_types.len == args.len); |
| 5709 | const symbol_index = self.bin_file.base.getGlobalSymbol(name) catch |err| { | 5707 | const symbol_index = func.bin_file.base.getGlobalSymbol(name) catch |err| { |
| 5710 | return self.fail("Could not find or create global symbol '{s}'", .{@errorName(err)}); | 5708 | return func.fail("Could not find or create global symbol '{s}'", .{@errorName(err)}); |
| 5711 | }; | 5709 | }; |
| 5712 | | 5710 | |
| 5713 | // Always pass over C-ABI | 5711 | // Always pass over C-ABI |
| 5714 | var func_type = try genFunctype(self.gpa, .C, param_types, return_type, self.target); | 5712 | var func_type = try genFunctype(func.gpa, .C, param_types, return_type, func.target); |
| 5715 | defer func_type.deinit(self.gpa); | 5713 | defer func_type.deinit(func.gpa); |
| 5716 | const func_type_index = try self.bin_file.putOrGetFuncType(func_type); | 5714 | const func_type_index = try func.bin_file.putOrGetFuncType(func_type); |
| 5717 | try self.bin_file.addOrUpdateImport(name, symbol_index, null, func_type_index); | 5715 | try func.bin_file.addOrUpdateImport(name, symbol_index, null, func_type_index); |
| 5718 | | 5716 | |
| 5719 | const want_sret_param = firstParamSRet(.C, return_type, self.target); | 5717 | const want_sret_param = firstParamSRet(.C, return_type, func.target); |
| 5720 | // if we want return as first param, we allocate a pointer to stack, | 5718 | // if we want return as first param, we allocate a pointer to stack, |
| 5721 | // and emit it as our first argument | 5719 | // and emit it as our first argument |
| 5722 | const sret = if (want_sret_param) blk: { | 5720 | const sret = if (want_sret_param) blk: { |
| 5723 | const sret_local = try self.allocStack(return_type); | 5721 | const sret_local = try func.allocStack(return_type); |
| 5724 | try self.lowerToStack(sret_local); | 5722 | try func.lowerToStack(sret_local); |
| 5725 | break :blk sret_local; | 5723 | break :blk sret_local; |
| 5726 | } else WValue{ .none = {} }; | 5724 | } else WValue{ .none = {} }; |
| 5727 | | 5725 | |
| ... | @@ -5729,16 +5727,16 @@ fn callIntrinsic( | ... | @@ -5729,16 +5727,16 @@ fn callIntrinsic( |
| 5729 | for (args) |arg, arg_i| { | 5727 | for (args) |arg, arg_i| { |
| 5730 | assert(!(want_sret_param and arg == .stack)); | 5728 | assert(!(want_sret_param and arg == .stack)); |
| 5731 | assert(param_types[arg_i].hasRuntimeBitsIgnoreComptime()); | 5729 | assert(param_types[arg_i].hasRuntimeBitsIgnoreComptime()); |
| 5732 | try self.lowerArg(.C, param_types[arg_i], arg); | 5730 | try func.lowerArg(.C, param_types[arg_i], arg); |
| 5733 | } | 5731 | } |
| 5734 | | 5732 | |
| 5735 | // Actually call our intrinsic | 5733 | // Actually call our intrinsic |
| 5736 | try self.addLabel(.call, symbol_index); | 5734 | try func.addLabel(.call, symbol_index); |
| 5737 | | 5735 | |
| 5738 | if (!return_type.hasRuntimeBitsIgnoreComptime()) { | 5736 | if (!return_type.hasRuntimeBitsIgnoreComptime()) { |
| 5739 | return WValue.none; | 5737 | return WValue.none; |
| 5740 | } else if (return_type.isNoReturn()) { | 5738 | } else if (return_type.isNoReturn()) { |
| 5741 | try self.addTag(.@"unreachable"); | 5739 | try func.addTag(.@"unreachable"); |
| 5742 | return WValue.none; | 5740 | return WValue.none; |
| 5743 | } else if (want_sret_param) { | 5741 | } else if (want_sret_param) { |
| 5744 | return sret; | 5742 | return sret; |