| author | |
| committer | |
| log | f95fcb2b1fadb34588f727f22b4d5ed07cd73d5e |
| tree | 04255c9c61f790c1bc67d770d7cd8299fd1cdc2d |
| parent | 23e981bbd1138bb7328d2cbb5a0480e26324088e |
| parent | c157b1987865892fa1acfe88208e3567048fc892 |
| signature |
stage2: handle more MCValue types in `struct_field_ptr` in x86_64 and pad out nonpacked struct fields when lowering to bytes (all targets incl wasm32)7 files changed, 200 insertions(+), 74 deletions(-)
src/arch/wasm/CodeGen.zig+41-28| ... | ... | @@ -619,7 +619,7 @@ fn resolveInst(self: *Self, ref: Air.Inst.Ref) InnerError!WValue { |
| 619 | 619 | .code = &value_bytes, |
| 620 | 620 | .symbol_index = try self.bin_file.createLocalSymbol(self.decl, ty), |
| 621 | 621 | }; |
| 622 | const result = decl_gen.genTypedValue(ty, val, value_bytes.writer()) catch |err| { | |
| 622 | const result = decl_gen.genTypedValue(ty, val) catch |err| { | |
| 623 | 623 | // When a codegen error occured, take ownership of the error message |
| 624 | 624 | if (err == error.CodegenFail) { |
| 625 | 625 | self.err_msg = decl_gen.err_msg; |
| ... | ... | @@ -907,14 +907,15 @@ pub const DeclGen = struct { |
| 907 | 907 | break :init_val payload.data.init; |
| 908 | 908 | } else decl.val; |
| 909 | 909 | if (init_val.tag() != .unreachable_value) { |
| 910 | return self.genTypedValue(decl.ty, init_val, self.code.writer()); | |
| 910 | return self.genTypedValue(decl.ty, init_val); | |
| 911 | 911 | } |
| 912 | 912 | return Result{ .appended = {} }; |
| 913 | 913 | } |
| 914 | 914 | } |
| 915 | 915 | |
| 916 | 916 | /// Generates the wasm bytecode for the declaration belonging to `Context` |
| 917 | fn genTypedValue(self: *DeclGen, ty: Type, val: Value, writer: anytype) InnerError!Result { | |
| 917 | fn genTypedValue(self: *DeclGen, ty: Type, val: Value) InnerError!Result { | |
| 918 | const writer = self.code.writer(); | |
| 918 | 919 | if (val.isUndef()) { |
| 919 | 920 | try writer.writeByteNTimes(0xaa, @intCast(usize, ty.abiSize(self.target()))); |
| 920 | 921 | return Result{ .appended = {} }; |
| ... | ... | @@ -926,7 +927,7 @@ pub const DeclGen = struct { |
| 926 | 927 | .function => val.castTag(.function).?.data.owner_decl, |
| 927 | 928 | else => unreachable, |
| 928 | 929 | }; |
| 929 | return try self.lowerDeclRef(ty, val, fn_decl, writer); | |
| 930 | return try self.lowerDeclRef(ty, val, fn_decl); | |
| 930 | 931 | }, |
| 931 | 932 | .Optional => { |
| 932 | 933 | var opt_buf: Type.Payload.ElemType = undefined; |
| ... | ... | @@ -942,9 +943,9 @@ pub const DeclGen = struct { |
| 942 | 943 | |
| 943 | 944 | if (ty.isPtrLikeOptional()) { |
| 944 | 945 | if (val.castTag(.opt_payload)) |payload| { |
| 945 | return self.genTypedValue(payload_type, payload.data, writer); | |
| 946 | return self.genTypedValue(payload_type, payload.data); | |
| 946 | 947 | } else if (!val.isNull()) { |
| 947 | return self.genTypedValue(payload_type, val, writer); | |
| 948 | return self.genTypedValue(payload_type, val); | |
| 948 | 949 | } else { |
| 949 | 950 | try writer.writeByteNTimes(0, abi_size); |
| 950 | 951 | return Result{ .appended = {} }; |
| ... | ... | @@ -956,7 +957,6 @@ pub const DeclGen = struct { |
| 956 | 957 | switch (try self.genTypedValue( |
| 957 | 958 | payload_type, |
| 958 | 959 | if (val.castTag(.opt_payload)) |pl| pl.data else Value.initTag(.undef), |
| 959 | writer, | |
| 960 | 960 | )) { |
| 961 | 961 | .appended => {}, |
| 962 | 962 | .externally_managed => |payload| try writer.writeAll(payload), |
| ... | ... | @@ -972,7 +972,7 @@ pub const DeclGen = struct { |
| 972 | 972 | const elem_vals = val.castTag(.array).?.data; |
| 973 | 973 | const elem_ty = ty.childType(); |
| 974 | 974 | for (elem_vals) |elem_val| { |
| 975 | switch (try self.genTypedValue(elem_ty, elem_val, writer)) { | |
| 975 | switch (try self.genTypedValue(elem_ty, elem_val)) { | |
| 976 | 976 | .appended => {}, |
| 977 | 977 | .externally_managed => |data| try writer.writeAll(data), |
| 978 | 978 | } |
| ... | ... | @@ -987,20 +987,20 @@ pub const DeclGen = struct { |
| 987 | 987 | |
| 988 | 988 | var index: u32 = 0; |
| 989 | 989 | while (index < len) : (index += 1) { |
| 990 | switch (try self.genTypedValue(elem_ty, array, writer)) { | |
| 990 | switch (try self.genTypedValue(elem_ty, array)) { | |
| 991 | 991 | .externally_managed => |data| try writer.writeAll(data), |
| 992 | 992 | .appended => {}, |
| 993 | 993 | } |
| 994 | 994 | } |
| 995 | 995 | if (sentinel) |sentinel_value| { |
| 996 | return self.genTypedValue(elem_ty, sentinel_value, writer); | |
| 996 | return self.genTypedValue(elem_ty, sentinel_value); | |
| 997 | 997 | } |
| 998 | 998 | return Result{ .appended = {} }; |
| 999 | 999 | }, |
| 1000 | 1000 | .empty_array_sentinel => { |
| 1001 | 1001 | const elem_ty = ty.childType(); |
| 1002 | 1002 | const sent_val = ty.sentinel().?; |
| 1003 | return self.genTypedValue(elem_ty, sent_val, writer); | |
| 1003 | return self.genTypedValue(elem_ty, sent_val); | |
| 1004 | 1004 | }, |
| 1005 | 1005 | else => unreachable, |
| 1006 | 1006 | }, |
| ... | ... | @@ -1037,25 +1037,37 @@ pub const DeclGen = struct { |
| 1037 | 1037 | const int_val = val.enumToInt(ty, &int_buffer); |
| 1038 | 1038 | var buf: Type.Payload.Bits = undefined; |
| 1039 | 1039 | const int_ty = ty.intTagType(&buf); |
| 1040 | return self.genTypedValue(int_ty, int_val, writer); | |
| 1040 | return self.genTypedValue(int_ty, int_val); | |
| 1041 | 1041 | }, |
| 1042 | 1042 | .Bool => { |
| 1043 | 1043 | try writer.writeByte(@boolToInt(val.toBool())); |
| 1044 | 1044 | return Result{ .appended = {} }; |
| 1045 | 1045 | }, |
| 1046 | 1046 | .Struct => { |
| 1047 | const struct_ty = ty.castTag(.@"struct").?.data; | |
| 1048 | if (struct_ty.layout == .Packed) { | |
| 1047 | const struct_obj = ty.castTag(.@"struct").?.data; | |
| 1048 | if (struct_obj.layout == .Packed) { | |
| 1049 | 1049 | return self.fail("TODO: Packed structs for wasm", .{}); |
| 1050 | 1050 | } |
| 1051 | ||
| 1052 | const struct_begin = self.code.items.len; | |
| 1051 | 1053 | const field_vals = val.castTag(.@"struct").?.data; |
| 1052 | 1054 | for (field_vals) |field_val, index| { |
| 1053 | 1055 | const field_ty = ty.structFieldType(index); |
| 1054 | 1056 | if (!field_ty.hasRuntimeBits()) continue; |
| 1055 | switch (try self.genTypedValue(field_ty, field_val, writer)) { | |
| 1057 | ||
| 1058 | switch (try self.genTypedValue(field_ty, field_val)) { | |
| 1056 | 1059 | .appended => {}, |
| 1057 | 1060 | .externally_managed => |payload| try writer.writeAll(payload), |
| 1058 | 1061 | } |
| 1062 | const unpadded_field_len = self.code.items.len - struct_begin; | |
| 1063 | ||
| 1064 | // Pad struct members if required | |
| 1065 | const padded_field_end = ty.structFieldOffset(index + 1, self.target()); | |
| 1066 | const padding = try std.math.cast(usize, padded_field_end - unpadded_field_len); | |
| 1067 | ||
| 1068 | if (padding > 0) { | |
| 1069 | try writer.writeByteNTimes(0, padding); | |
| 1070 | } | |
| 1059 | 1071 | } |
| 1060 | 1072 | return Result{ .appended = {} }; |
| 1061 | 1073 | }, |
| ... | ... | @@ -1064,12 +1076,12 @@ pub const DeclGen = struct { |
| 1064 | 1076 | const layout = ty.unionGetLayout(self.target()); |
| 1065 | 1077 | |
| 1066 | 1078 | if (layout.payload_size == 0) { |
| 1067 | return self.genTypedValue(ty.unionTagType().?, union_val.tag, writer); | |
| 1079 | return self.genTypedValue(ty.unionTagType().?, union_val.tag); | |
| 1068 | 1080 | } |
| 1069 | 1081 | |
| 1070 | 1082 | // Check if we should store the tag first, in which case, do so now: |
| 1071 | 1083 | if (layout.tag_align >= layout.payload_align) { |
| 1072 | switch (try self.genTypedValue(ty.unionTagType().?, union_val.tag, writer)) { | |
| 1084 | switch (try self.genTypedValue(ty.unionTagType().?, union_val.tag)) { | |
| 1073 | 1085 | .appended => {}, |
| 1074 | 1086 | .externally_managed => |payload| try writer.writeAll(payload), |
| 1075 | 1087 | } |
| ... | ... | @@ -1082,7 +1094,7 @@ pub const DeclGen = struct { |
| 1082 | 1094 | if (!field_ty.hasRuntimeBits()) { |
| 1083 | 1095 | try writer.writeByteNTimes(0xaa, @intCast(usize, layout.payload_size)); |
| 1084 | 1096 | } else { |
| 1085 | switch (try self.genTypedValue(field_ty, union_val.val, writer)) { | |
| 1097 | switch (try self.genTypedValue(field_ty, union_val.val)) { | |
| 1086 | 1098 | .appended => {}, |
| 1087 | 1099 | .externally_managed => |payload| try writer.writeAll(payload), |
| 1088 | 1100 | } |
| ... | ... | @@ -1098,26 +1110,26 @@ pub const DeclGen = struct { |
| 1098 | 1110 | if (layout.tag_size == 0) { |
| 1099 | 1111 | return Result{ .appended = {} }; |
| 1100 | 1112 | } |
| 1101 | return self.genTypedValue(union_ty.tag_ty, union_val.tag, writer); | |
| 1113 | return self.genTypedValue(union_ty.tag_ty, union_val.tag); | |
| 1102 | 1114 | }, |
| 1103 | 1115 | .Pointer => switch (val.tag()) { |
| 1104 | 1116 | .variable => { |
| 1105 | 1117 | const decl = val.castTag(.variable).?.data.owner_decl; |
| 1106 | return self.lowerDeclRef(ty, val, decl, writer); | |
| 1118 | return self.lowerDeclRef(ty, val, decl); | |
| 1107 | 1119 | }, |
| 1108 | 1120 | .decl_ref => { |
| 1109 | 1121 | const decl = val.castTag(.decl_ref).?.data; |
| 1110 | return self.lowerDeclRef(ty, val, decl, writer); | |
| 1122 | return self.lowerDeclRef(ty, val, decl); | |
| 1111 | 1123 | }, |
| 1112 | 1124 | .slice => { |
| 1113 | 1125 | const slice = val.castTag(.slice).?.data; |
| 1114 | 1126 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 1115 | 1127 | const ptr_ty = ty.slicePtrFieldType(&buf); |
| 1116 | switch (try self.genTypedValue(ptr_ty, slice.ptr, writer)) { | |
| 1128 | switch (try self.genTypedValue(ptr_ty, slice.ptr)) { | |
| 1117 | 1129 | .externally_managed => |data| try writer.writeAll(data), |
| 1118 | 1130 | .appended => {}, |
| 1119 | 1131 | } |
| 1120 | switch (try self.genTypedValue(Type.usize, slice.len, writer)) { | |
| 1132 | switch (try self.genTypedValue(Type.usize, slice.len)) { | |
| 1121 | 1133 | .externally_managed => |data| try writer.writeAll(data), |
| 1122 | 1134 | .appended => {}, |
| 1123 | 1135 | } |
| ... | ... | @@ -1135,14 +1147,14 @@ pub const DeclGen = struct { |
| 1135 | 1147 | const is_pl = val.errorUnionIsPayload(); |
| 1136 | 1148 | |
| 1137 | 1149 | const err_val = if (!is_pl) val else Value.initTag(.zero); |
| 1138 | switch (try self.genTypedValue(error_ty, err_val, writer)) { | |
| 1150 | switch (try self.genTypedValue(error_ty, err_val)) { | |
| 1139 | 1151 | .externally_managed => |data| try writer.writeAll(data), |
| 1140 | 1152 | .appended => {}, |
| 1141 | 1153 | } |
| 1142 | 1154 | |
| 1143 | 1155 | if (payload_ty.hasRuntimeBits()) { |
| 1144 | 1156 | const pl_val = if (val.castTag(.eu_payload)) |pl| pl.data else Value.initTag(.undef); |
| 1145 | switch (try self.genTypedValue(payload_ty, pl_val, writer)) { | |
| 1157 | switch (try self.genTypedValue(payload_ty, pl_val)) { | |
| 1146 | 1158 | .externally_managed => |data| try writer.writeAll(data), |
| 1147 | 1159 | .appended => {}, |
| 1148 | 1160 | } |
| ... | ... | @@ -1167,11 +1179,12 @@ pub const DeclGen = struct { |
| 1167 | 1179 | } |
| 1168 | 1180 | } |
| 1169 | 1181 | |
| 1170 | fn lowerDeclRef(self: *DeclGen, ty: Type, val: Value, decl: *Module.Decl, writer: anytype) InnerError!Result { | |
| 1182 | fn lowerDeclRef(self: *DeclGen, ty: Type, val: Value, decl: *Module.Decl) InnerError!Result { | |
| 1183 | const writer = self.code.writer(); | |
| 1171 | 1184 | if (ty.isSlice()) { |
| 1172 | 1185 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 1173 | 1186 | const slice_ty = ty.slicePtrFieldType(&buf); |
| 1174 | switch (try self.genTypedValue(slice_ty, val, writer)) { | |
| 1187 | switch (try self.genTypedValue(slice_ty, val)) { | |
| 1175 | 1188 | .appended => {}, |
| 1176 | 1189 | .externally_managed => |payload| try writer.writeAll(payload), |
| 1177 | 1190 | } |
| ... | ... | @@ -1179,7 +1192,7 @@ pub const DeclGen = struct { |
| 1179 | 1192 | .base = .{ .tag = .int_u64 }, |
| 1180 | 1193 | .data = val.sliceLen(), |
| 1181 | 1194 | }; |
| 1182 | return self.genTypedValue(Type.usize, Value.initPayload(&slice_len.base), writer); | |
| 1195 | return self.genTypedValue(Type.usize, Value.initPayload(&slice_len.base)); | |
| 1183 | 1196 | } |
| 1184 | 1197 | |
| 1185 | 1198 | decl.markAlive(); |
src/arch/x86_64/CodeGen.zig+76-40| ... | ... | @@ -680,6 +680,9 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void { |
| 680 | 680 | .wrap_errunion_err => try self.airWrapErrUnionErr(inst), |
| 681 | 681 | // zig fmt: on |
| 682 | 682 | } |
| 683 | ||
| 684 | assert(!self.register_manager.frozenRegsExist()); | |
| 685 | ||
| 683 | 686 | if (std.debug.runtime_safety) { |
| 684 | 687 | if (self.air_bookkeeping < old_air_bookkeeping + 1) { |
| 685 | 688 | std.debug.panic("in codegen.zig, handling of AIR instruction %{d} ('{}') did not do proper bookkeeping. Look for a missing call to finishAir.", .{ inst, air_tags[inst] }); |
| ... | ... | @@ -809,7 +812,7 @@ pub fn spillInstruction(self: *Self, reg: Register, inst: Air.Inst.Index) !void |
| 809 | 812 | const stack_mcv = try self.allocRegOrMem(inst, false); |
| 810 | 813 | log.debug("spilling {d} to stack mcv {any}", .{ inst, stack_mcv }); |
| 811 | 814 | const reg_mcv = self.getResolvedInstValue(inst); |
| 812 | assert(reg == reg_mcv.register.to64()); | |
| 815 | assert(reg.to64() == reg_mcv.register.to64()); | |
| 813 | 816 | const branch = &self.branch_stack.items[self.branch_stack.items.len - 1]; |
| 814 | 817 | try branch.inst_table.put(self.gpa, inst, stack_mcv); |
| 815 | 818 | try self.genSetStack(self.air.typeOfIndex(inst), stack_mcv.stack_offset, reg_mcv); |
| ... | ... | @@ -827,9 +830,9 @@ fn copyToTmpRegister(self: *Self, ty: Type, mcv: MCValue) !Register { |
| 827 | 830 | /// Allocates a new register and copies `mcv` into it. |
| 828 | 831 | /// `reg_owner` is the instruction that gets associated with the register in the register table. |
| 829 | 832 | /// This can have a side effect of spilling instructions to the stack to free up a register. |
| 830 | fn copyToNewRegister(self: *Self, reg_owner: Air.Inst.Index, mcv: MCValue) !MCValue { | |
| 833 | fn copyToNewRegister(self: *Self, reg_owner: Air.Inst.Index, ty: Type, mcv: MCValue) !MCValue { | |
| 831 | 834 | const reg = try self.register_manager.allocReg(reg_owner, &.{}); |
| 832 | try self.genSetReg(self.air.typeOfIndex(reg_owner), reg, mcv); | |
| 835 | try self.genSetReg(ty, reg, mcv); | |
| 833 | 836 | return MCValue{ .register = reg }; |
| 834 | 837 | } |
| 835 | 838 | |
| ... | ... | @@ -838,11 +841,12 @@ fn copyToNewRegister(self: *Self, reg_owner: Air.Inst.Index, mcv: MCValue) !MCVa |
| 838 | 841 | fn copyToNewRegisterWithExceptions( |
| 839 | 842 | self: *Self, |
| 840 | 843 | reg_owner: Air.Inst.Index, |
| 844 | ty: Type, | |
| 841 | 845 | mcv: MCValue, |
| 842 | 846 | exceptions: []const Register, |
| 843 | 847 | ) !MCValue { |
| 844 | 848 | const reg = try self.register_manager.allocReg(reg_owner, exceptions); |
| 845 | try self.genSetReg(self.air.typeOfIndex(reg_owner), reg, mcv); | |
| 849 | try self.genSetReg(ty, reg, mcv); | |
| 846 | 850 | return MCValue{ .register = reg }; |
| 847 | 851 | } |
| 848 | 852 | |
| ... | ... | @@ -892,13 +896,10 @@ fn airIntCast(self: *Self, inst: Air.Inst.Index) !void { |
| 892 | 896 | if (operand_abi_size > 8 or dest_abi_size > 8) { |
| 893 | 897 | return self.fail("TODO implement intCast for abi sizes larger than 8", .{}); |
| 894 | 898 | } |
| 895 | const reg = switch (operand) { | |
| 896 | .register => |src_reg| try self.register_manager.allocReg(inst, &.{src_reg}), | |
| 897 | else => try self.register_manager.allocReg(inst, &.{}), | |
| 898 | }; | |
| 899 | try self.genSetReg(dest_ty, reg, .{ .immediate = 0 }); | |
| 900 | try self.genSetReg(dest_ty, reg, operand); | |
| 901 | break :blk .{ .register = registerAlias(reg, @intCast(u32, dest_abi_size)) }; | |
| 899 | ||
| 900 | if (operand.isRegister()) self.register_manager.freezeRegs(&.{operand.register}); | |
| 901 | defer if (operand.isRegister()) self.register_manager.unfreezeRegs(&.{operand.register}); | |
| 902 | break :blk try self.copyToNewRegister(inst, dest_ty, operand); | |
| 902 | 903 | }; |
| 903 | 904 | |
| 904 | 905 | return self.finishAir(inst, dst_mcv, .{ ty_op.operand, .none, .none }); |
| ... | ... | @@ -1208,7 +1209,7 @@ fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) !void { |
| 1208 | 1209 | if (self.reuseOperand(inst, ty_op.operand, 0, operand)) { |
| 1209 | 1210 | break :result operand; |
| 1210 | 1211 | } |
| 1211 | break :result try self.copyToNewRegister(inst, operand); | |
| 1212 | break :result try self.copyToNewRegister(inst, self.air.typeOfIndex(inst), operand); | |
| 1212 | 1213 | }; |
| 1213 | 1214 | return self.finishAir(inst, result, .{ ty_op.operand, .none, .none }); |
| 1214 | 1215 | } |
| ... | ... | @@ -1479,16 +1480,11 @@ fn airPtrElemPtr(self: *Self, inst: Air.Inst.Index) !void { |
| 1479 | 1480 | const index_ty = self.air.typeOf(extra.rhs); |
| 1480 | 1481 | const index = try self.resolveInst(extra.rhs); |
| 1481 | 1482 | const offset_reg = try self.elemOffset(index_ty, index, elem_abi_size); |
| 1482 | const dst_mcv = blk: { | |
| 1483 | switch (ptr) { | |
| 1484 | .ptr_stack_offset => { | |
| 1485 | const reg = try self.register_manager.allocReg(inst, &.{offset_reg}); | |
| 1486 | try self.genSetReg(ptr_ty, reg, ptr); | |
| 1487 | break :blk .{ .register = reg }; | |
| 1488 | }, | |
| 1489 | else => return self.fail("TODO implement ptr_elem_ptr when ptr is {}", .{ptr}), | |
| 1490 | } | |
| 1491 | }; | |
| 1483 | ||
| 1484 | self.register_manager.freezeRegs(&.{offset_reg}); | |
| 1485 | defer self.register_manager.unfreezeRegs(&.{offset_reg}); | |
| 1486 | ||
| 1487 | const dst_mcv = try self.copyToNewRegister(inst, ptr_ty, ptr); | |
| 1492 | 1488 | try self.genBinMathOpMir(.add, ptr_ty, dst_mcv, .{ .register = offset_reg }); |
| 1493 | 1489 | break :result dst_mcv; |
| 1494 | 1490 | }; |
| ... | ... | @@ -1795,22 +1791,62 @@ fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u8) !void { |
| 1795 | 1791 | } |
| 1796 | 1792 | |
| 1797 | 1793 | fn structFieldPtr(self: *Self, inst: Air.Inst.Index, operand: Air.Inst.Ref, index: u32) !MCValue { |
| 1798 | return if (self.liveness.isUnused(inst)) .dead else result: { | |
| 1799 | const mcv = try self.resolveInst(operand); | |
| 1800 | const struct_ty = self.air.typeOf(operand).childType(); | |
| 1801 | const struct_size = @intCast(i32, struct_ty.abiSize(self.target.*)); | |
| 1802 | const struct_field_offset = @intCast(i32, struct_ty.structFieldOffset(index, self.target.*)); | |
| 1803 | const struct_field_ty = struct_ty.structFieldType(index); | |
| 1804 | const struct_field_size = @intCast(i32, struct_field_ty.abiSize(self.target.*)); | |
| 1805 | ||
| 1794 | if (self.liveness.isUnused(inst)) { | |
| 1795 | return MCValue.dead; | |
| 1796 | } | |
| 1797 | const mcv = try self.resolveInst(operand); | |
| 1798 | const ptr_ty = self.air.typeOf(operand); | |
| 1799 | const struct_ty = ptr_ty.childType(); | |
| 1800 | const struct_size = @intCast(u32, struct_ty.abiSize(self.target.*)); | |
| 1801 | const struct_field_offset = @intCast(u32, struct_ty.structFieldOffset(index, self.target.*)); | |
| 1802 | const struct_field_ty = struct_ty.structFieldType(index); | |
| 1803 | const struct_field_size = @intCast(u32, struct_field_ty.abiSize(self.target.*)); | |
| 1804 | ||
| 1805 | const dst_mcv: MCValue = result: { | |
| 1806 | 1806 | switch (mcv) { |
| 1807 | .stack_offset => { | |
| 1808 | const offset_reg = try self.copyToTmpRegister(ptr_ty, .{ | |
| 1809 | .immediate = struct_field_offset, | |
| 1810 | }); | |
| 1811 | self.register_manager.freezeRegs(&.{offset_reg}); | |
| 1812 | defer self.register_manager.unfreezeRegs(&.{offset_reg}); | |
| 1813 | ||
| 1814 | const dst_mcv = try self.copyToNewRegister(inst, ptr_ty, mcv); | |
| 1815 | try self.genBinMathOpMir(.add, ptr_ty, dst_mcv, .{ .register = offset_reg }); | |
| 1816 | break :result dst_mcv; | |
| 1817 | }, | |
| 1807 | 1818 | .ptr_stack_offset => |off| { |
| 1808 | const ptr_stack_offset = off + struct_size - struct_field_offset - struct_field_size; | |
| 1819 | const offset_to_field = struct_size - struct_field_offset - struct_field_size; | |
| 1820 | const ptr_stack_offset = off + @intCast(i32, offset_to_field); | |
| 1809 | 1821 | break :result MCValue{ .ptr_stack_offset = ptr_stack_offset }; |
| 1810 | 1822 | }, |
| 1823 | .register => |reg| { | |
| 1824 | const offset_reg = try self.copyToTmpRegister(ptr_ty, .{ | |
| 1825 | .immediate = struct_field_offset, | |
| 1826 | }); | |
| 1827 | self.register_manager.freezeRegs(&.{offset_reg}); | |
| 1828 | defer self.register_manager.unfreezeRegs(&.{offset_reg}); | |
| 1829 | ||
| 1830 | const can_reuse_operand = self.reuseOperand(inst, operand, 0, mcv); | |
| 1831 | const result_reg = blk: { | |
| 1832 | if (can_reuse_operand) { | |
| 1833 | break :blk reg; | |
| 1834 | } else { | |
| 1835 | self.register_manager.freezeRegs(&.{reg}); | |
| 1836 | const result_reg = try self.register_manager.allocReg(inst, &.{}); | |
| 1837 | try self.genSetReg(ptr_ty, result_reg, mcv); | |
| 1838 | break :blk result_reg; | |
| 1839 | } | |
| 1840 | }; | |
| 1841 | defer if (!can_reuse_operand) self.register_manager.unfreezeRegs(&.{reg}); | |
| 1842 | ||
| 1843 | try self.genBinMathOpMir(.add, ptr_ty, .{ .register = result_reg }, .{ .register = offset_reg }); | |
| 1844 | break :result MCValue{ .register = result_reg }; | |
| 1845 | }, | |
| 1811 | 1846 | else => return self.fail("TODO implement codegen struct_field_ptr for {}", .{mcv}), |
| 1812 | 1847 | } |
| 1813 | 1848 | }; |
| 1849 | return dst_mcv; | |
| 1814 | 1850 | } |
| 1815 | 1851 | |
| 1816 | 1852 | fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) !void { |
| ... | ... | @@ -1859,13 +1895,14 @@ fn genBinMathOp(self: *Self, inst: Air.Inst.Index, op_lhs: Air.Inst.Ref, op_rhs: |
| 1859 | 1895 | // Source operand can be an immediate, 8 bits or 32 bits. |
| 1860 | 1896 | // So, if either one of the operands dies with this instruction, we can use it |
| 1861 | 1897 | // as the result MCValue. |
| 1898 | const dst_ty = self.air.typeOfIndex(inst); | |
| 1862 | 1899 | var dst_mcv: MCValue = undefined; |
| 1863 | 1900 | var src_mcv: MCValue = undefined; |
| 1864 | 1901 | if (self.reuseOperand(inst, op_lhs, 0, lhs)) { |
| 1865 | 1902 | // LHS dies; use it as the destination. |
| 1866 | 1903 | // Both operands cannot be memory. |
| 1867 | 1904 | if (lhs.isMemory() and rhs.isMemory()) { |
| 1868 | dst_mcv = try self.copyToNewRegister(inst, lhs); | |
| 1905 | dst_mcv = try self.copyToNewRegister(inst, dst_ty, lhs); | |
| 1869 | 1906 | src_mcv = rhs; |
| 1870 | 1907 | } else { |
| 1871 | 1908 | dst_mcv = lhs; |
| ... | ... | @@ -1875,7 +1912,7 @@ fn genBinMathOp(self: *Self, inst: Air.Inst.Index, op_lhs: Air.Inst.Ref, op_rhs: |
| 1875 | 1912 | // RHS dies; use it as the destination. |
| 1876 | 1913 | // Both operands cannot be memory. |
| 1877 | 1914 | if (lhs.isMemory() and rhs.isMemory()) { |
| 1878 | dst_mcv = try self.copyToNewRegister(inst, rhs); | |
| 1915 | dst_mcv = try self.copyToNewRegister(inst, dst_ty, rhs); | |
| 1879 | 1916 | src_mcv = lhs; |
| 1880 | 1917 | } else { |
| 1881 | 1918 | dst_mcv = rhs; |
| ... | ... | @@ -1887,18 +1924,18 @@ fn genBinMathOp(self: *Self, inst: Air.Inst.Index, op_lhs: Air.Inst.Ref, op_rhs: |
| 1887 | 1924 | // If the allocated register is the same as the rhs register, don't allocate that one |
| 1888 | 1925 | // and instead spill a subsequent one. Otherwise, this can result in a miscompilation |
| 1889 | 1926 | // in the presence of several binary operations performed in a single block. |
| 1890 | try self.copyToNewRegisterWithExceptions(inst, lhs, &.{rhs.register}) | |
| 1927 | try self.copyToNewRegisterWithExceptions(inst, dst_ty, lhs, &.{rhs.register}) | |
| 1891 | 1928 | else |
| 1892 | try self.copyToNewRegister(inst, lhs); | |
| 1929 | try self.copyToNewRegister(inst, dst_ty, lhs); | |
| 1893 | 1930 | src_mcv = rhs; |
| 1894 | 1931 | } else { |
| 1895 | 1932 | dst_mcv = if (lhs.isRegister()) |
| 1896 | 1933 | // If the allocated register is the same as the rhs register, don't allocate that one |
| 1897 | 1934 | // and instead spill a subsequent one. Otherwise, this can result in a miscompilation |
| 1898 | 1935 | // in the presence of several binary operations performed in a single block. |
| 1899 | try self.copyToNewRegisterWithExceptions(inst, rhs, &.{lhs.register}) | |
| 1936 | try self.copyToNewRegisterWithExceptions(inst, dst_ty, rhs, &.{lhs.register}) | |
| 1900 | 1937 | else |
| 1901 | try self.copyToNewRegister(inst, rhs); | |
| 1938 | try self.copyToNewRegister(inst, dst_ty, rhs); | |
| 1902 | 1939 | src_mcv = lhs; |
| 1903 | 1940 | } |
| 1904 | 1941 | } |
| ... | ... | @@ -1917,7 +1954,6 @@ fn genBinMathOp(self: *Self, inst: Air.Inst.Index, op_lhs: Air.Inst.Ref, op_rhs: |
| 1917 | 1954 | } |
| 1918 | 1955 | |
| 1919 | 1956 | // Now for step 2, we assing an MIR instruction |
| 1920 | const dst_ty = self.air.typeOfIndex(inst); | |
| 1921 | 1957 | const air_tags = self.air.instructions.items(.tag); |
| 1922 | 1958 | switch (air_tags[inst]) { |
| 1923 | 1959 | .add, .addwrap, .ptr_add => try self.genBinMathOpMir(.add, dst_ty, dst_mcv, src_mcv), |
| ... | ... | @@ -2417,7 +2453,7 @@ fn airCall(self: *Self, inst: Air.Inst.Index) !void { |
| 2417 | 2453 | .register => |reg| { |
| 2418 | 2454 | if (Register.allocIndex(reg) == null) { |
| 2419 | 2455 | // Save function return value in a callee saved register |
| 2420 | break :result try self.copyToNewRegister(inst, info.return_value); | |
| 2456 | break :result try self.copyToNewRegister(inst, self.air.typeOfIndex(inst), info.return_value); | |
| 2421 | 2457 | } |
| 2422 | 2458 | }, |
| 2423 | 2459 | else => {}, |
| ... | ... | @@ -2494,7 +2530,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { |
| 2494 | 2530 | // Either one, but not both, can be a memory operand. |
| 2495 | 2531 | // Source operand can be an immediate, 8 bits or 32 bits. |
| 2496 | 2532 | const dst_mcv = if (lhs.isImmediate() or (lhs.isMemory() and rhs.isMemory())) |
| 2497 | try self.copyToNewRegister(inst, lhs) | |
| 2533 | try self.copyToNewRegister(inst, ty, lhs) | |
| 2498 | 2534 | else |
| 2499 | 2535 | lhs; |
| 2500 | 2536 | // This instruction supports only signed 32-bit immediates at most. |
src/codegen.zig+24-1| ... | ... | @@ -373,11 +373,24 @@ pub fn generateSymbol( |
| 373 | 373 | }, |
| 374 | 374 | .Struct => { |
| 375 | 375 | // TODO debug info |
| 376 | // TODO padding of struct members | |
| 376 | const struct_obj = typed_value.ty.castTag(.@"struct").?.data; | |
| 377 | if (struct_obj.layout == .Packed) { | |
| 378 | return Result{ | |
| 379 | .fail = try ErrorMsg.create( | |
| 380 | bin_file.allocator, | |
| 381 | src_loc, | |
| 382 | "TODO implement generateSymbol for packed struct", | |
| 383 | .{}, | |
| 384 | ), | |
| 385 | }; | |
| 386 | } | |
| 387 | ||
| 388 | const struct_begin = code.items.len; | |
| 377 | 389 | const field_vals = typed_value.val.castTag(.@"struct").?.data; |
| 378 | 390 | for (field_vals) |field_val, index| { |
| 379 | 391 | const field_ty = typed_value.ty.structFieldType(index); |
| 380 | 392 | if (!field_ty.hasRuntimeBits()) continue; |
| 393 | ||
| 381 | 394 | switch (try generateSymbol(bin_file, src_loc, .{ |
| 382 | 395 | .ty = field_ty, |
| 383 | 396 | .val = field_val, |
| ... | ... | @@ -388,6 +401,16 @@ pub fn generateSymbol( |
| 388 | 401 | }, |
| 389 | 402 | .fail => |em| return Result{ .fail = em }, |
| 390 | 403 | } |
| 404 | const unpadded_field_end = code.items.len - struct_begin; | |
| 405 | ||
| 406 | // Pad struct members if required | |
| 407 | const target = bin_file.options.target; | |
| 408 | const padded_field_end = typed_value.ty.structFieldOffset(index + 1, target); | |
| 409 | const padding = try math.cast(usize, padded_field_end - unpadded_field_end); | |
| 410 | ||
| 411 | if (padding > 0) { | |
| 412 | try code.writer().writeByteNTimes(0, padding); | |
| 413 | } | |
| 391 | 414 | } |
| 392 | 415 | |
| 393 | 416 | return Result{ .appended = {} }; |
src/link/Elf.zig+1| ... | ... | @@ -2367,6 +2367,7 @@ fn deinitRelocs(gpa: Allocator, table: *File.DbgInfoTypeRelocsTable) void { |
| 2367 | 2367 | } |
| 2368 | 2368 | |
| 2369 | 2369 | fn updateDeclCode(self: *Elf, decl: *Module.Decl, code: []const u8, stt_bits: u8) !*elf.Elf64_Sym { |
| 2370 | log.debug("updateDeclCode {s}{*}", .{ mem.sliceTo(decl.name, 0), decl }); | |
| 2370 | 2371 | const required_alignment = decl.ty.abiAlignment(self.base.options.target); |
| 2371 | 2372 | |
| 2372 | 2373 | const block_list = self.getDeclBlockList(decl); |
test/behavior.zig+1-1| ... | ... | @@ -34,6 +34,7 @@ test { |
| 34 | 34 | _ = @import("behavior/slice_sentinel_comptime.zig"); |
| 35 | 35 | _ = @import("behavior/type.zig"); |
| 36 | 36 | _ = @import("behavior/truncate.zig"); |
| 37 | _ = @import("behavior/struct.zig"); | |
| 37 | 38 | |
| 38 | 39 | if (builtin.zig_backend != .stage2_arm and builtin.zig_backend != .stage2_x86_64) { |
| 39 | 40 | // Tests that pass for stage1, llvm backend, C backend, wasm backend. |
| ... | ... | @@ -69,7 +70,6 @@ test { |
| 69 | 70 | _ = @import("behavior/ptrcast.zig"); |
| 70 | 71 | _ = @import("behavior/ref_var_in_if_after_if_2nd_switch_prong.zig"); |
| 71 | 72 | _ = @import("behavior/src.zig"); |
| 72 | _ = @import("behavior/struct.zig"); | |
| 73 | 73 | _ = @import("behavior/this.zig"); |
| 74 | 74 | _ = @import("behavior/try.zig"); |
| 75 | 75 | _ = @import("behavior/type_info.zig"); |
test/behavior/cast.zig-4| ... | ... | @@ -5,8 +5,6 @@ const maxInt = std.math.maxInt; |
| 5 | 5 | const builtin = @import("builtin"); |
| 6 | 6 | |
| 7 | 7 | test "int to ptr cast" { |
| 8 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; | |
| 9 | ||
| 10 | 8 | const x = @as(usize, 13); |
| 11 | 9 | const y = @intToPtr(*u8, x); |
| 12 | 10 | const z = @ptrToInt(y); |
| ... | ... | @@ -14,8 +12,6 @@ test "int to ptr cast" { |
| 14 | 12 | } |
| 15 | 13 | |
| 16 | 14 | test "integer literal to pointer cast" { |
| 17 | if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; | |
| 18 | ||
| 19 | 15 | const vga_mem = @intToPtr(*u16, 0xB8000); |
| 20 | 16 | try expect(@ptrToInt(vga_mem) == 0xB8000); |
| 21 | 17 | } |
test/behavior/struct.zig+57| ... | ... | @@ -9,6 +9,8 @@ const maxInt = std.math.maxInt; |
| 9 | 9 | top_level_field: i32, |
| 10 | 10 | |
| 11 | 11 | test "top level fields" { |
| 12 | if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 13 | ||
| 12 | 14 | var instance = @This(){ |
| 13 | 15 | .top_level_field = 1234, |
| 14 | 16 | }; |
| ... | ... | @@ -16,6 +18,39 @@ test "top level fields" { |
| 16 | 18 | try expect(@as(i32, 1235) == instance.top_level_field); |
| 17 | 19 | } |
| 18 | 20 | |
| 21 | const StructWithFields = struct { | |
| 22 | a: u8, | |
| 23 | b: u32, | |
| 24 | c: u64, | |
| 25 | d: u32, | |
| 26 | ||
| 27 | fn first(self: *const StructWithFields) u8 { | |
| 28 | return self.a; | |
| 29 | } | |
| 30 | ||
| 31 | fn second(self: *const StructWithFields) u32 { | |
| 32 | return self.b; | |
| 33 | } | |
| 34 | ||
| 35 | fn third(self: *const StructWithFields) u64 { | |
| 36 | return self.c; | |
| 37 | } | |
| 38 | ||
| 39 | fn fourth(self: *const StructWithFields) u32 { | |
| 40 | return self.d; | |
| 41 | } | |
| 42 | }; | |
| 43 | ||
| 44 | test "non-packed struct has fields padded out to the required alignment" { | |
| 45 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 46 | ||
| 47 | const foo = StructWithFields{ .a = 5, .b = 1, .c = 10, .d = 2 }; | |
| 48 | try expect(foo.first() == 5); | |
| 49 | try expect(foo.second() == 1); | |
| 50 | try expect(foo.third() == 10); | |
| 51 | try expect(foo.fourth() == 2); | |
| 52 | } | |
| 53 | ||
| 19 | 54 | const StructWithNoFields = struct { |
| 20 | 55 | fn add(a: i32, b: i32) i32 { |
| 21 | 56 | return a + b; |
| ... | ... | @@ -29,6 +64,8 @@ const StructFoo = struct { |
| 29 | 64 | }; |
| 30 | 65 | |
| 31 | 66 | test "structs" { |
| 67 | if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 68 | ||
| 32 | 69 | var foo: StructFoo = undefined; |
| 33 | 70 | @memset(@ptrCast([*]u8, &foo), 0, @sizeOf(StructFoo)); |
| 34 | 71 | foo.a += 1; |
| ... | ... | @@ -45,6 +82,8 @@ fn testMutation(foo: *StructFoo) void { |
| 45 | 82 | } |
| 46 | 83 | |
| 47 | 84 | test "struct byval assign" { |
| 85 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 86 | ||
| 48 | 87 | var foo1: StructFoo = undefined; |
| 49 | 88 | var foo2: StructFoo = undefined; |
| 50 | 89 | |
| ... | ... | @@ -56,6 +95,8 @@ test "struct byval assign" { |
| 56 | 95 | } |
| 57 | 96 | |
| 58 | 97 | test "call struct static method" { |
| 98 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 99 | ||
| 59 | 100 | const result = StructWithNoFields.add(3, 4); |
| 60 | 101 | try expect(result == 7); |
| 61 | 102 | } |
| ... | ... | @@ -85,6 +126,8 @@ const Val = struct { |
| 85 | 126 | }; |
| 86 | 127 | |
| 87 | 128 | test "fn call of struct field" { |
| 129 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 130 | ||
| 88 | 131 | const Foo = struct { |
| 89 | 132 | ptr: fn () i32, |
| 90 | 133 | }; |
| ... | ... | @@ -114,12 +157,16 @@ const MemberFnTestFoo = struct { |
| 114 | 157 | }; |
| 115 | 158 | |
| 116 | 159 | test "call member function directly" { |
| 160 | if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 161 | ||
| 117 | 162 | const instance = MemberFnTestFoo{ .x = 1234 }; |
| 118 | 163 | const result = MemberFnTestFoo.member(instance); |
| 119 | 164 | try expect(result == 1234); |
| 120 | 165 | } |
| 121 | 166 | |
| 122 | 167 | test "store member function in variable" { |
| 168 | if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 169 | ||
| 123 | 170 | const instance = MemberFnTestFoo{ .x = 1234 }; |
| 124 | 171 | const memberFn = MemberFnTestFoo.member; |
| 125 | 172 | const result = memberFn(instance); |
| ... | ... | @@ -127,6 +174,8 @@ test "store member function in variable" { |
| 127 | 174 | } |
| 128 | 175 | |
| 129 | 176 | test "member functions" { |
| 177 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 178 | ||
| 130 | 179 | const r = MemberFnRand{ .seed = 1234 }; |
| 131 | 180 | try expect(r.getSeed() == 1234); |
| 132 | 181 | } |
| ... | ... | @@ -138,6 +187,8 @@ const MemberFnRand = struct { |
| 138 | 187 | }; |
| 139 | 188 | |
| 140 | 189 | test "return struct byval from function" { |
| 190 | if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 191 | ||
| 141 | 192 | const bar = makeBar2(1234, 5678); |
| 142 | 193 | try expect(bar.y == 5678); |
| 143 | 194 | } |
| ... | ... | @@ -153,6 +204,8 @@ fn makeBar2(x: i32, y: i32) Bar { |
| 153 | 204 | } |
| 154 | 205 | |
| 155 | 206 | test "call method with mutable reference to struct with no fields" { |
| 207 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 208 | ||
| 156 | 209 | const S = struct { |
| 157 | 210 | fn doC(s: *const @This()) bool { |
| 158 | 211 | _ = s; |
| ... | ... | @@ -172,6 +225,8 @@ test "call method with mutable reference to struct with no fields" { |
| 172 | 225 | } |
| 173 | 226 | |
| 174 | 227 | test "usingnamespace within struct scope" { |
| 228 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 229 | ||
| 175 | 230 | const S = struct { |
| 176 | 231 | usingnamespace struct { |
| 177 | 232 | pub fn inner() i32 { |
| ... | ... | @@ -183,6 +238,8 @@ test "usingnamespace within struct scope" { |
| 183 | 238 | } |
| 184 | 239 | |
| 185 | 240 | test "struct field init with catch" { |
| 241 | if (builtin.zig_backend == .stage2_x86_64 or builtin.zig_backend == .stage2_arm) return error.SkipZigTest; | |
| 242 | ||
| 186 | 243 | const S = struct { |
| 187 | 244 | fn doTheTest() !void { |
| 188 | 245 | var x: anyerror!isize = 1; |