| author | |
| committer | |
| log | e8813b296bc55a13b534bd9b2a03e1f6af366915 |
| tree | d58db370235d221e8780b06e85dccfb548536b3a |
| parent | cb6364624fb28bf51adb2dc16c8e93a30c33c76b |
| parent | 44f9061b718e9bdcd43d258291f89930b74aa56a |
| signature |
stage2: lazy `@alignOf`26 files changed, 1733 insertions(+), 1094 deletions(-)
src/Compilation.zig+3-1| ... | ... | @@ -2781,7 +2781,9 @@ fn processOneJob(comp: *Compilation, job: Job, main_progress_node: *std.Progress |
| 2781 | 2781 | .error_msg = null, |
| 2782 | 2782 | .decl = decl, |
| 2783 | 2783 | .fwd_decl = fwd_decl.toManaged(gpa), |
| 2784 | .typedefs = c_codegen.TypedefMap.init(gpa), | |
| 2784 | .typedefs = c_codegen.TypedefMap.initContext(gpa, .{ | |
| 2785 | .target = comp.getTarget(), | |
| 2786 | }), | |
| 2785 | 2787 | .typedefs_arena = typedefs_arena.allocator(), |
| 2786 | 2788 | }; |
| 2787 | 2789 | defer dg.fwd_decl.deinit(); |
src/Module.zig+25-33| ... | ... | @@ -146,6 +146,8 @@ const MonomorphedFuncsSet = std.HashMapUnmanaged( |
| 146 | 146 | ); |
| 147 | 147 | |
| 148 | 148 | const MonomorphedFuncsContext = struct { |
| 149 | target: Target, | |
| 150 | ||
| 149 | 151 | pub fn eql(ctx: @This(), a: *Fn, b: *Fn) bool { |
| 150 | 152 | _ = ctx; |
| 151 | 153 | return a == b; |
| ... | ... | @@ -153,7 +155,6 @@ const MonomorphedFuncsContext = struct { |
| 153 | 155 | |
| 154 | 156 | /// Must match `Sema.GenericCallAdapter.hash`. |
| 155 | 157 | pub fn hash(ctx: @This(), key: *Fn) u64 { |
| 156 | _ = ctx; | |
| 157 | 158 | var hasher = std.hash.Wyhash.init(0); |
| 158 | 159 | |
| 159 | 160 | // The generic function Decl is guaranteed to be the first dependency |
| ... | ... | @@ -168,7 +169,7 @@ const MonomorphedFuncsContext = struct { |
| 168 | 169 | const generic_ty_info = generic_owner_decl.ty.fnInfo(); |
| 169 | 170 | for (generic_ty_info.param_types) |param_ty, i| { |
| 170 | 171 | if (generic_ty_info.paramIsComptime(i) and param_ty.tag() != .generic_poison) { |
| 171 | comptime_args[i].val.hash(param_ty, &hasher); | |
| 172 | comptime_args[i].val.hash(param_ty, &hasher, ctx.target); | |
| 172 | 173 | } |
| 173 | 174 | } |
| 174 | 175 | |
| ... | ... | @@ -176,6 +177,12 @@ const MonomorphedFuncsContext = struct { |
| 176 | 177 | } |
| 177 | 178 | }; |
| 178 | 179 | |
| 180 | pub const WipAnalysis = struct { | |
| 181 | sema: *Sema, | |
| 182 | block: *Sema.Block, | |
| 183 | src: Module.LazySrcLoc, | |
| 184 | }; | |
| 185 | ||
| 179 | 186 | pub const MemoizedCallSet = std.HashMapUnmanaged( |
| 180 | 187 | MemoizedCall.Key, |
| 181 | 188 | MemoizedCall.Result, |
| ... | ... | @@ -184,6 +191,8 @@ pub const MemoizedCallSet = std.HashMapUnmanaged( |
| 184 | 191 | ); |
| 185 | 192 | |
| 186 | 193 | pub const MemoizedCall = struct { |
| 194 | target: std.Target, | |
| 195 | ||
| 187 | 196 | pub const Key = struct { |
| 188 | 197 | func: *Fn, |
| 189 | 198 | args: []TypedValue, |
| ... | ... | @@ -195,14 +204,12 @@ pub const MemoizedCall = struct { |
| 195 | 204 | }; |
| 196 | 205 | |
| 197 | 206 | pub fn eql(ctx: @This(), a: Key, b: Key) bool { |
| 198 | _ = ctx; | |
| 199 | ||
| 200 | 207 | if (a.func != b.func) return false; |
| 201 | 208 | |
| 202 | 209 | assert(a.args.len == b.args.len); |
| 203 | 210 | for (a.args) |a_arg, arg_i| { |
| 204 | 211 | const b_arg = b.args[arg_i]; |
| 205 | if (!a_arg.eql(b_arg)) { | |
| 212 | if (!a_arg.eql(b_arg, ctx.target)) { | |
| 206 | 213 | return false; |
| 207 | 214 | } |
| 208 | 215 | } |
| ... | ... | @@ -212,8 +219,6 @@ pub const MemoizedCall = struct { |
| 212 | 219 | |
| 213 | 220 | /// Must match `Sema.GenericCallAdapter.hash`. |
| 214 | 221 | pub fn hash(ctx: @This(), key: Key) u64 { |
| 215 | _ = ctx; | |
| 216 | ||
| 217 | 222 | var hasher = std.hash.Wyhash.init(0); |
| 218 | 223 | |
| 219 | 224 | // The generic function Decl is guaranteed to be the first dependency |
| ... | ... | @@ -223,7 +228,7 @@ pub const MemoizedCall = struct { |
| 223 | 228 | // This logic must be kept in sync with the logic in `analyzeCall` that |
| 224 | 229 | // computes the hash. |
| 225 | 230 | for (key.args) |arg| { |
| 226 | arg.hash(&hasher); | |
| 231 | arg.hash(&hasher, ctx.target); | |
| 227 | 232 | } |
| 228 | 233 | |
| 229 | 234 | return hasher.final(); |
| ... | ... | @@ -1230,7 +1235,7 @@ pub const Union = struct { |
| 1230 | 1235 | if (field.abi_align == 0) { |
| 1231 | 1236 | break :a field.ty.abiAlignment(target); |
| 1232 | 1237 | } else { |
| 1233 | break :a @intCast(u32, field.abi_align.toUnsignedInt()); | |
| 1238 | break :a field.abi_align; | |
| 1234 | 1239 | } |
| 1235 | 1240 | }; |
| 1236 | 1241 | if (field_align > most_alignment) { |
| ... | ... | @@ -3877,6 +3882,7 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool { |
| 3877 | 3882 | const bytes = try sema.resolveConstString(&block_scope, src, linksection_ref); |
| 3878 | 3883 | break :blk (try decl_arena_allocator.dupeZ(u8, bytes)).ptr; |
| 3879 | 3884 | }; |
| 3885 | const target = sema.mod.getTarget(); | |
| 3880 | 3886 | const address_space = blk: { |
| 3881 | 3887 | const addrspace_ctx: Sema.AddressSpaceContext = switch (decl_tv.val.tag()) { |
| 3882 | 3888 | .function, .extern_fn => .function, |
| ... | ... | @@ -3886,9 +3892,9 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool { |
| 3886 | 3892 | |
| 3887 | 3893 | break :blk switch (decl.zirAddrspaceRef()) { |
| 3888 | 3894 | .none => switch (addrspace_ctx) { |
| 3889 | .function => target_util.defaultAddressSpace(sema.mod.getTarget(), .function), | |
| 3890 | .variable => target_util.defaultAddressSpace(sema.mod.getTarget(), .global_mutable), | |
| 3891 | .constant => target_util.defaultAddressSpace(sema.mod.getTarget(), .global_constant), | |
| 3895 | .function => target_util.defaultAddressSpace(target, .function), | |
| 3896 | .variable => target_util.defaultAddressSpace(target, .global_mutable), | |
| 3897 | .constant => target_util.defaultAddressSpace(target, .global_constant), | |
| 3892 | 3898 | else => unreachable, |
| 3893 | 3899 | }, |
| 3894 | 3900 | else => |addrspace_ref| try sema.analyzeAddrspace(&block_scope, src, addrspace_ref, addrspace_ctx), |
| ... | ... | @@ -3904,13 +3910,15 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool { |
| 3904 | 3910 | |
| 3905 | 3911 | if (decl.is_usingnamespace) { |
| 3906 | 3912 | const ty_ty = Type.initTag(.type); |
| 3907 | if (!decl_tv.ty.eql(ty_ty)) { | |
| 3908 | return sema.fail(&block_scope, src, "expected type, found {}", .{decl_tv.ty}); | |
| 3913 | if (!decl_tv.ty.eql(ty_ty, target)) { | |
| 3914 | return sema.fail(&block_scope, src, "expected type, found {}", .{ | |
| 3915 | decl_tv.ty.fmt(target), | |
| 3916 | }); | |
| 3909 | 3917 | } |
| 3910 | 3918 | var buffer: Value.ToTypeBuffer = undefined; |
| 3911 | 3919 | const ty = decl_tv.val.toType(&buffer); |
| 3912 | 3920 | if (ty.getNamespace() == null) { |
| 3913 | return sema.fail(&block_scope, src, "type {} has no namespace", .{ty}); | |
| 3921 | return sema.fail(&block_scope, src, "type {} has no namespace", .{ty.fmt(target)}); | |
| 3914 | 3922 | } |
| 3915 | 3923 | |
| 3916 | 3924 | decl.ty = ty_ty; |
| ... | ... | @@ -3937,7 +3945,7 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool { |
| 3937 | 3945 | |
| 3938 | 3946 | if (decl.has_tv) { |
| 3939 | 3947 | prev_type_has_bits = decl.ty.isFnOrHasRuntimeBits(); |
| 3940 | type_changed = !decl.ty.eql(decl_tv.ty); | |
| 3948 | type_changed = !decl.ty.eql(decl_tv.ty, target); | |
| 3941 | 3949 | if (decl.getFunction()) |prev_func| { |
| 3942 | 3950 | prev_is_inline = prev_func.state == .inline_only; |
| 3943 | 3951 | } |
| ... | ... | @@ -3986,7 +3994,7 @@ fn semaDecl(mod: *Module, decl: *Decl) !bool { |
| 3986 | 3994 | } |
| 3987 | 3995 | var type_changed = true; |
| 3988 | 3996 | if (decl.has_tv) { |
| 3989 | type_changed = !decl.ty.eql(decl_tv.ty); | |
| 3997 | type_changed = !decl.ty.eql(decl_tv.ty, target); | |
| 3990 | 3998 | decl.clearValues(gpa); |
| 3991 | 3999 | } |
| 3992 | 4000 | |
| ... | ... | @@ -5054,22 +5062,6 @@ pub fn errNoteNonLazy( |
| 5054 | 5062 | }; |
| 5055 | 5063 | } |
| 5056 | 5064 | |
| 5057 | pub fn errorUnionType( | |
| 5058 | arena: Allocator, | |
| 5059 | error_set: Type, | |
| 5060 | payload: Type, | |
| 5061 | ) Allocator.Error!Type { | |
| 5062 | assert(error_set.zigTypeTag() == .ErrorSet); | |
| 5063 | if (error_set.eql(Type.initTag(.anyerror)) and payload.eql(Type.initTag(.void))) { | |
| 5064 | return Type.initTag(.anyerror_void_error_union); | |
| 5065 | } | |
| 5066 | ||
| 5067 | return Type.Tag.error_union.create(arena, .{ | |
| 5068 | .error_set = error_set, | |
| 5069 | .payload = payload, | |
| 5070 | }); | |
| 5071 | } | |
| 5072 | ||
| 5073 | 5065 | pub fn getTarget(mod: Module) Target { |
| 5074 | 5066 | return mod.comp.bin_file.options.target; |
| 5075 | 5067 | } |
src/RangeSet.zig+22-9| ... | ... | @@ -6,6 +6,7 @@ const RangeSet = @This(); |
| 6 | 6 | const SwitchProngSrc = @import("Module.zig").SwitchProngSrc; |
| 7 | 7 | |
| 8 | 8 | ranges: std.ArrayList(Range), |
| 9 | target: std.Target, | |
| 9 | 10 | |
| 10 | 11 | pub const Range = struct { |
| 11 | 12 | first: Value, |
| ... | ... | @@ -13,9 +14,10 @@ pub const Range = struct { |
| 13 | 14 | src: SwitchProngSrc, |
| 14 | 15 | }; |
| 15 | 16 | |
| 16 | pub fn init(allocator: std.mem.Allocator) RangeSet { | |
| 17 | pub fn init(allocator: std.mem.Allocator, target: std.Target) RangeSet { | |
| 17 | 18 | return .{ |
| 18 | 19 | .ranges = std.ArrayList(Range).init(allocator), |
| 20 | .target = target, | |
| 19 | 21 | }; |
| 20 | 22 | } |
| 21 | 23 | |
| ... | ... | @@ -30,8 +32,12 @@ pub fn add( |
| 30 | 32 | ty: Type, |
| 31 | 33 | src: SwitchProngSrc, |
| 32 | 34 | ) !?SwitchProngSrc { |
| 35 | const target = self.target; | |
| 36 | ||
| 33 | 37 | for (self.ranges.items) |range| { |
| 34 | if (last.compare(.gte, range.first, ty) and first.compare(.lte, range.last, ty)) { | |
| 38 | if (last.compare(.gte, range.first, ty, target) and | |
| 39 | first.compare(.lte, range.last, ty, target)) | |
| 40 | { | |
| 35 | 41 | return range.src; // They overlap. |
| 36 | 42 | } |
| 37 | 43 | } |
| ... | ... | @@ -43,19 +49,26 @@ pub fn add( |
| 43 | 49 | return null; |
| 44 | 50 | } |
| 45 | 51 | |
| 52 | const LessThanContext = struct { ty: Type, target: std.Target }; | |
| 53 | ||
| 46 | 54 | /// Assumes a and b do not overlap |
| 47 | fn lessThan(ty: Type, a: Range, b: Range) bool { | |
| 48 | return a.first.compare(.lt, b.first, ty); | |
| 55 | fn lessThan(ctx: LessThanContext, a: Range, b: Range) bool { | |
| 56 | return a.first.compare(.lt, b.first, ctx.ty, ctx.target); | |
| 49 | 57 | } |
| 50 | 58 | |
| 51 | 59 | pub fn spans(self: *RangeSet, first: Value, last: Value, ty: Type) !bool { |
| 52 | 60 | if (self.ranges.items.len == 0) |
| 53 | 61 | return false; |
| 54 | 62 | |
| 55 | std.sort.sort(Range, self.ranges.items, ty, lessThan); | |
| 63 | const target = self.target; | |
| 64 | ||
| 65 | std.sort.sort(Range, self.ranges.items, LessThanContext{ | |
| 66 | .ty = ty, | |
| 67 | .target = target, | |
| 68 | }, lessThan); | |
| 56 | 69 | |
| 57 | if (!self.ranges.items[0].first.eql(first, ty) or | |
| 58 | !self.ranges.items[self.ranges.items.len - 1].last.eql(last, ty)) | |
| 70 | if (!self.ranges.items[0].first.eql(first, ty, target) or | |
| 71 | !self.ranges.items[self.ranges.items.len - 1].last.eql(last, ty, target)) | |
| 59 | 72 | { |
| 60 | 73 | return false; |
| 61 | 74 | } |
| ... | ... | @@ -71,10 +84,10 @@ pub fn spans(self: *RangeSet, first: Value, last: Value, ty: Type) !bool { |
| 71 | 84 | const prev = self.ranges.items[i]; |
| 72 | 85 | |
| 73 | 86 | // prev.last + 1 == cur.first |
| 74 | try counter.copy(prev.last.toBigInt(&space)); | |
| 87 | try counter.copy(prev.last.toBigInt(&space, target)); | |
| 75 | 88 | try counter.addScalar(counter.toConst(), 1); |
| 76 | 89 | |
| 77 | const cur_start_int = cur.first.toBigInt(&space); | |
| 90 | const cur_start_int = cur.first.toBigInt(&space, target); | |
| 78 | 91 | if (!cur_start_int.eq(counter.toConst())) { |
| 79 | 92 | return false; |
| 80 | 93 | } |
src/Sema.zig+541-366| ... | ... | @@ -1303,7 +1303,8 @@ pub fn resolveConstString( |
| 1303 | 1303 | const wanted_type = Type.initTag(.const_slice_u8); |
| 1304 | 1304 | const coerced_inst = try sema.coerce(block, wanted_type, air_inst, src); |
| 1305 | 1305 | const val = try sema.resolveConstValue(block, src, coerced_inst); |
| 1306 | return val.toAllocatedBytes(wanted_type, sema.arena); | |
| 1306 | const target = sema.mod.getTarget(); | |
| 1307 | return val.toAllocatedBytes(wanted_type, sema.arena, target); | |
| 1307 | 1308 | } |
| 1308 | 1309 | |
| 1309 | 1310 | pub fn resolveType(sema: *Sema, block: *Block, src: LazySrcLoc, zir_ref: Zir.Inst.Ref) !Type { |
| ... | ... | @@ -1457,19 +1458,29 @@ fn failWithDivideByZero(sema: *Sema, block: *Block, src: LazySrcLoc) CompileErro |
| 1457 | 1458 | } |
| 1458 | 1459 | |
| 1459 | 1460 | fn failWithModRemNegative(sema: *Sema, block: *Block, src: LazySrcLoc, lhs_ty: Type, rhs_ty: Type) CompileError { |
| 1460 | return sema.fail(block, src, "remainder division with '{}' and '{}': signed integers and floats must use @rem or @mod", .{ lhs_ty, rhs_ty }); | |
| 1461 | const target = sema.mod.getTarget(); | |
| 1462 | return sema.fail(block, src, "remainder division with '{}' and '{}': signed integers and floats must use @rem or @mod", .{ | |
| 1463 | lhs_ty.fmt(target), rhs_ty.fmt(target), | |
| 1464 | }); | |
| 1461 | 1465 | } |
| 1462 | 1466 | |
| 1463 | 1467 | fn failWithExpectedOptionalType(sema: *Sema, block: *Block, src: LazySrcLoc, optional_ty: Type) CompileError { |
| 1464 | return sema.fail(block, src, "expected optional type, found {}", .{optional_ty}); | |
| 1468 | const target = sema.mod.getTarget(); | |
| 1469 | return sema.fail(block, src, "expected optional type, found {}", .{optional_ty.fmt(target)}); | |
| 1465 | 1470 | } |
| 1466 | 1471 | |
| 1467 | 1472 | fn failWithArrayInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError { |
| 1468 | return sema.fail(block, src, "type '{}' does not support array initialization syntax", .{ty}); | |
| 1473 | const target = sema.mod.getTarget(); | |
| 1474 | return sema.fail(block, src, "type '{}' does not support array initialization syntax", .{ | |
| 1475 | ty.fmt(target), | |
| 1476 | }); | |
| 1469 | 1477 | } |
| 1470 | 1478 | |
| 1471 | 1479 | fn failWithStructInitNotSupported(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError { |
| 1472 | return sema.fail(block, src, "type '{}' does not support struct initialization syntax", .{ty}); | |
| 1480 | const target = sema.mod.getTarget(); | |
| 1481 | return sema.fail(block, src, "type '{}' does not support struct initialization syntax", .{ | |
| 1482 | ty.fmt(target), | |
| 1483 | }); | |
| 1473 | 1484 | } |
| 1474 | 1485 | |
| 1475 | 1486 | fn failWithErrorSetCodeMissing( |
| ... | ... | @@ -1479,8 +1490,9 @@ fn failWithErrorSetCodeMissing( |
| 1479 | 1490 | dest_err_set_ty: Type, |
| 1480 | 1491 | src_err_set_ty: Type, |
| 1481 | 1492 | ) CompileError { |
| 1493 | const target = sema.mod.getTarget(); | |
| 1482 | 1494 | return sema.fail(block, src, "expected type '{}', found type '{}'", .{ |
| 1483 | dest_err_set_ty, src_err_set_ty, | |
| 1495 | dest_err_set_ty.fmt(target), src_err_set_ty.fmt(target), | |
| 1484 | 1496 | }); |
| 1485 | 1497 | } |
| 1486 | 1498 | |
| ... | ... | @@ -1578,8 +1590,8 @@ fn resolveInt( |
| 1578 | 1590 | const air_inst = sema.resolveInst(zir_ref); |
| 1579 | 1591 | const coerced = try sema.coerce(block, dest_ty, air_inst, src); |
| 1580 | 1592 | const val = try sema.resolveConstValue(block, src, coerced); |
| 1581 | ||
| 1582 | return val.toUnsignedInt(); | |
| 1593 | const target = sema.mod.getTarget(); | |
| 1594 | return (try val.getUnsignedIntAdvanced(target, sema.kit(block, src))).?; | |
| 1583 | 1595 | } |
| 1584 | 1596 | |
| 1585 | 1597 | // Returns a compile error if the value has tag `variable`. See `resolveInstValue` for |
| ... | ... | @@ -1864,6 +1876,7 @@ fn createTypeName( |
| 1864 | 1876 | }, |
| 1865 | 1877 | .parent => return sema.gpa.dupeZ(u8, mem.sliceTo(block.src_decl.name, 0)), |
| 1866 | 1878 | .func => { |
| 1879 | const target = sema.mod.getTarget(); | |
| 1867 | 1880 | const fn_info = sema.code.getFnInfo(sema.func.?.zir_body_inst); |
| 1868 | 1881 | const zir_tags = sema.code.instructions.items(.tag); |
| 1869 | 1882 | |
| ... | ... | @@ -1881,7 +1894,7 @@ fn createTypeName( |
| 1881 | 1894 | const arg_val = sema.resolveConstMaybeUndefVal(block, .unneeded, arg) catch unreachable; |
| 1882 | 1895 | |
| 1883 | 1896 | if (arg_i != 0) try buf.appendSlice(","); |
| 1884 | try buf.writer().print("{}", .{arg_val.fmtValue(sema.typeOf(arg))}); | |
| 1897 | try buf.writer().print("{}", .{arg_val.fmtValue(sema.typeOf(arg), target)}); | |
| 1885 | 1898 | |
| 1886 | 1899 | arg_i += 1; |
| 1887 | 1900 | continue; |
| ... | ... | @@ -2045,6 +2058,7 @@ fn zirEnumDecl( |
| 2045 | 2058 | enum_obj.tag_ty_inferred = true; |
| 2046 | 2059 | } |
| 2047 | 2060 | } |
| 2061 | const target = mod.getTarget(); | |
| 2048 | 2062 | |
| 2049 | 2063 | try enum_obj.fields.ensureTotalCapacity(new_decl_arena_allocator, fields_len); |
| 2050 | 2064 | const any_values = for (sema.code.extra[body_end..][0..bit_bags_count]) |bag| { |
| ... | ... | @@ -2053,6 +2067,7 @@ fn zirEnumDecl( |
| 2053 | 2067 | if (any_values) { |
| 2054 | 2068 | try enum_obj.values.ensureTotalCapacityContext(new_decl_arena_allocator, fields_len, .{ |
| 2055 | 2069 | .ty = enum_obj.tag_ty, |
| 2070 | .target = target, | |
| 2056 | 2071 | }); |
| 2057 | 2072 | } |
| 2058 | 2073 | |
| ... | ... | @@ -2102,16 +2117,18 @@ fn zirEnumDecl( |
| 2102 | 2117 | const copied_tag_val = try tag_val.copy(new_decl_arena_allocator); |
| 2103 | 2118 | enum_obj.values.putAssumeCapacityNoClobberContext(copied_tag_val, {}, .{ |
| 2104 | 2119 | .ty = enum_obj.tag_ty, |
| 2120 | .target = target, | |
| 2105 | 2121 | }); |
| 2106 | 2122 | } else if (any_values) { |
| 2107 | 2123 | const tag_val = if (last_tag_val) |val| |
| 2108 | try val.intAdd(Value.one, enum_obj.tag_ty, sema.arena) | |
| 2124 | try val.intAdd(Value.one, enum_obj.tag_ty, sema.arena, target) | |
| 2109 | 2125 | else |
| 2110 | 2126 | Value.zero; |
| 2111 | 2127 | last_tag_val = tag_val; |
| 2112 | 2128 | const copied_tag_val = try tag_val.copy(new_decl_arena_allocator); |
| 2113 | 2129 | enum_obj.values.putAssumeCapacityNoClobberContext(copied_tag_val, {}, .{ |
| 2114 | 2130 | .ty = enum_obj.tag_ty, |
| 2131 | .target = target, | |
| 2115 | 2132 | }); |
| 2116 | 2133 | } |
| 2117 | 2134 | } |
| ... | ... | @@ -2417,13 +2434,14 @@ fn zirIndexablePtrLen(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE |
| 2417 | 2434 | else |
| 2418 | 2435 | object_ty; |
| 2419 | 2436 | |
| 2437 | const target = sema.mod.getTarget(); | |
| 2420 | 2438 | if (!array_ty.isIndexable()) { |
| 2421 | 2439 | const msg = msg: { |
| 2422 | 2440 | const msg = try sema.errMsg( |
| 2423 | 2441 | block, |
| 2424 | 2442 | src, |
| 2425 | 2443 | "type '{}' does not support indexing", |
| 2426 | .{array_ty}, | |
| 2444 | .{array_ty.fmt(target)}, | |
| 2427 | 2445 | ); |
| 2428 | 2446 | errdefer msg.destroy(sema.gpa); |
| 2429 | 2447 | try sema.errNote( |
| ... | ... | @@ -3346,8 +3364,9 @@ fn failWithBadMemberAccess( |
| 3346 | 3364 | else => unreachable, |
| 3347 | 3365 | }; |
| 3348 | 3366 | const msg = msg: { |
| 3367 | const target = sema.mod.getTarget(); | |
| 3349 | 3368 | const msg = try sema.errMsg(block, field_src, "{s} '{}' has no member named '{s}'", .{ |
| 3350 | kw_name, agg_ty, field_name, | |
| 3369 | kw_name, agg_ty.fmt(target), field_name, | |
| 3351 | 3370 | }); |
| 3352 | 3371 | errdefer msg.destroy(sema.gpa); |
| 3353 | 3372 | try sema.addDeclaredHereNote(msg, agg_ty); |
| ... | ... | @@ -3680,6 +3699,7 @@ fn zirCompileLog( |
| 3680 | 3699 | const src_node = extra.data.src_node; |
| 3681 | 3700 | const src: LazySrcLoc = .{ .node_offset = src_node }; |
| 3682 | 3701 | const args = sema.code.refSlice(extra.end, extended.small); |
| 3702 | const target = sema.mod.getTarget(); | |
| 3683 | 3703 | |
| 3684 | 3704 | for (args) |arg_ref, i| { |
| 3685 | 3705 | if (i != 0) try writer.print(", ", .{}); |
| ... | ... | @@ -3687,9 +3707,11 @@ fn zirCompileLog( |
| 3687 | 3707 | const arg = sema.resolveInst(arg_ref); |
| 3688 | 3708 | const arg_ty = sema.typeOf(arg); |
| 3689 | 3709 | if (try sema.resolveMaybeUndefVal(block, src, arg)) |val| { |
| 3690 | try writer.print("@as({}, {})", .{ arg_ty, val.fmtValue(arg_ty) }); | |
| 3710 | try writer.print("@as({}, {})", .{ | |
| 3711 | arg_ty.fmt(target), val.fmtValue(arg_ty, target), | |
| 3712 | }); | |
| 3691 | 3713 | } else { |
| 3692 | try writer.print("@as({}, [runtime value])", .{arg_ty}); | |
| 3714 | try writer.print("@as({}, [runtime value])", .{arg_ty.fmt(target)}); | |
| 3693 | 3715 | } |
| 3694 | 3716 | } |
| 3695 | 3717 | try writer.print("\n", .{}); |
| ... | ... | @@ -3982,9 +4004,10 @@ fn analyzeBlockBody( |
| 3982 | 4004 | |
| 3983 | 4005 | const type_src = src; // TODO: better source location |
| 3984 | 4006 | const valid_rt = try sema.validateRunTimeType(child_block, type_src, resolved_ty, false); |
| 4007 | const target = sema.mod.getTarget(); | |
| 3985 | 4008 | if (!valid_rt) { |
| 3986 | 4009 | const msg = msg: { |
| 3987 | const msg = try sema.errMsg(child_block, type_src, "value with comptime only type '{}' depends on runtime control flow", .{resolved_ty}); | |
| 4010 | const msg = try sema.errMsg(child_block, type_src, "value with comptime only type '{}' depends on runtime control flow", .{resolved_ty.fmt(target)}); | |
| 3988 | 4011 | errdefer msg.destroy(sema.gpa); |
| 3989 | 4012 | |
| 3990 | 4013 | const runtime_src = child_block.runtime_cond orelse child_block.runtime_loop.?; |
| ... | ... | @@ -4012,7 +4035,7 @@ fn analyzeBlockBody( |
| 4012 | 4035 | const br_operand = sema.air_instructions.items(.data)[br].br.operand; |
| 4013 | 4036 | const br_operand_src = src; |
| 4014 | 4037 | const br_operand_ty = sema.typeOf(br_operand); |
| 4015 | if (br_operand_ty.eql(resolved_ty)) { | |
| 4038 | if (br_operand_ty.eql(resolved_ty, target)) { | |
| 4016 | 4039 | // No type coercion needed. |
| 4017 | 4040 | continue; |
| 4018 | 4041 | } |
| ... | ... | @@ -4102,12 +4125,15 @@ pub fn analyzeExport( |
| 4102 | 4125 | ) !void { |
| 4103 | 4126 | const Export = Module.Export; |
| 4104 | 4127 | const mod = sema.mod; |
| 4128 | const target = mod.getTarget(); | |
| 4105 | 4129 | |
| 4106 | 4130 | try mod.ensureDeclAnalyzed(exported_decl); |
| 4107 | 4131 | // TODO run the same checks as we do for C ABI struct fields |
| 4108 | 4132 | switch (exported_decl.ty.zigTypeTag()) { |
| 4109 | 4133 | .Fn, .Int, .Enum, .Struct, .Union, .Array, .Float => {}, |
| 4110 | else => return sema.fail(block, src, "unable to export type '{}'", .{exported_decl.ty}), | |
| 4134 | else => return sema.fail(block, src, "unable to export type '{}'", .{ | |
| 4135 | exported_decl.ty.fmt(target), | |
| 4136 | }), | |
| 4111 | 4137 | } |
| 4112 | 4138 | |
| 4113 | 4139 | const gpa = mod.gpa; |
| ... | ... | @@ -4520,6 +4546,7 @@ const GenericCallAdapter = struct { |
| 4520 | 4546 | precomputed_hash: u64, |
| 4521 | 4547 | func_ty_info: Type.Payload.Function.Data, |
| 4522 | 4548 | comptime_tvs: []const TypedValue, |
| 4549 | target: std.Target, | |
| 4523 | 4550 | |
| 4524 | 4551 | pub fn eql(ctx: @This(), adapted_key: void, other_key: *Module.Fn) bool { |
| 4525 | 4552 | _ = adapted_key; |
| ... | ... | @@ -4532,7 +4559,7 @@ const GenericCallAdapter = struct { |
| 4532 | 4559 | for (other_comptime_args[0..ctx.func_ty_info.param_types.len]) |other_arg, i| { |
| 4533 | 4560 | if (other_arg.ty.tag() != .generic_poison) { |
| 4534 | 4561 | // anytype parameter |
| 4535 | if (!other_arg.ty.eql(ctx.comptime_tvs[i].ty)) { | |
| 4562 | if (!other_arg.ty.eql(ctx.comptime_tvs[i].ty, ctx.target)) { | |
| 4536 | 4563 | return false; |
| 4537 | 4564 | } |
| 4538 | 4565 | } |
| ... | ... | @@ -4543,7 +4570,7 @@ const GenericCallAdapter = struct { |
| 4543 | 4570 | // but the callsite does not. |
| 4544 | 4571 | return false; |
| 4545 | 4572 | } |
| 4546 | if (!other_arg.val.eql(ctx.comptime_tvs[i].val, other_arg.ty)) { | |
| 4573 | if (!other_arg.val.eql(ctx.comptime_tvs[i].val, other_arg.ty, ctx.target)) { | |
| 4547 | 4574 | return false; |
| 4548 | 4575 | } |
| 4549 | 4576 | } |
| ... | ... | @@ -4588,6 +4615,7 @@ fn analyzeCall( |
| 4588 | 4615 | const mod = sema.mod; |
| 4589 | 4616 | |
| 4590 | 4617 | const callee_ty = sema.typeOf(func); |
| 4618 | const target = sema.mod.getTarget(); | |
| 4591 | 4619 | const func_ty = func_ty: { |
| 4592 | 4620 | switch (callee_ty.zigTypeTag()) { |
| 4593 | 4621 | .Fn => break :func_ty callee_ty, |
| ... | ... | @@ -4599,7 +4627,7 @@ fn analyzeCall( |
| 4599 | 4627 | }, |
| 4600 | 4628 | else => {}, |
| 4601 | 4629 | } |
| 4602 | return sema.fail(block, func_src, "type '{}' not a function", .{callee_ty}); | |
| 4630 | return sema.fail(block, func_src, "type '{}' not a function", .{callee_ty.fmt(target)}); | |
| 4603 | 4631 | }; |
| 4604 | 4632 | |
| 4605 | 4633 | const func_ty_info = func_ty.fnInfo(); |
| ... | ... | @@ -4873,7 +4901,7 @@ fn analyzeCall( |
| 4873 | 4901 | // bug generating invalid LLVM IR. |
| 4874 | 4902 | const res2: Air.Inst.Ref = res2: { |
| 4875 | 4903 | if (should_memoize and is_comptime_call) { |
| 4876 | if (mod.memoized_calls.get(memoized_call_key)) |result| { | |
| 4904 | if (mod.memoized_calls.getContext(memoized_call_key, .{ .target = target })) |result| { | |
| 4877 | 4905 | const ty_inst = try sema.addType(fn_ret_ty); |
| 4878 | 4906 | try sema.air_values.append(gpa, result.val); |
| 4879 | 4907 | sema.air_instructions.set(block_inst, .{ |
| ... | ... | @@ -4945,10 +4973,10 @@ fn analyzeCall( |
| 4945 | 4973 | arg.* = try arg.*.copy(arena); |
| 4946 | 4974 | } |
| 4947 | 4975 | |
| 4948 | try mod.memoized_calls.put(gpa, memoized_call_key, .{ | |
| 4976 | try mod.memoized_calls.putContext(gpa, memoized_call_key, .{ | |
| 4949 | 4977 | .val = try result_val.copy(arena), |
| 4950 | 4978 | .arena = arena_allocator.state, |
| 4951 | }); | |
| 4979 | }, .{ .target = sema.mod.getTarget() }); | |
| 4952 | 4980 | delete_memoized_call_key = false; |
| 4953 | 4981 | } |
| 4954 | 4982 | } |
| ... | ... | @@ -5037,6 +5065,7 @@ fn instantiateGenericCall( |
| 5037 | 5065 | std.hash.autoHash(&hasher, @ptrToInt(module_fn)); |
| 5038 | 5066 | |
| 5039 | 5067 | const comptime_tvs = try sema.arena.alloc(TypedValue, func_ty_info.param_types.len); |
| 5068 | const target = sema.mod.getTarget(); | |
| 5040 | 5069 | |
| 5041 | 5070 | for (func_ty_info.param_types) |param_ty, i| { |
| 5042 | 5071 | const is_comptime = func_ty_info.paramIsComptime(i); |
| ... | ... | @@ -5045,7 +5074,7 @@ fn instantiateGenericCall( |
| 5045 | 5074 | const casted_arg = try sema.coerce(block, param_ty, uncasted_args[i], arg_src); |
| 5046 | 5075 | if (try sema.resolveMaybeUndefVal(block, arg_src, casted_arg)) |arg_val| { |
| 5047 | 5076 | if (param_ty.tag() != .generic_poison) { |
| 5048 | arg_val.hash(param_ty, &hasher); | |
| 5077 | arg_val.hash(param_ty, &hasher, target); | |
| 5049 | 5078 | } |
| 5050 | 5079 | comptime_tvs[i] = .{ |
| 5051 | 5080 | // This will be different than `param_ty` in the case of `generic_poison`. |
| ... | ... | @@ -5070,8 +5099,9 @@ fn instantiateGenericCall( |
| 5070 | 5099 | .precomputed_hash = precomputed_hash, |
| 5071 | 5100 | .func_ty_info = func_ty_info, |
| 5072 | 5101 | .comptime_tvs = comptime_tvs, |
| 5102 | .target = target, | |
| 5073 | 5103 | }; |
| 5074 | const gop = try mod.monomorphed_funcs.getOrPutAdapted(gpa, {}, adapter); | |
| 5104 | const gop = try mod.monomorphed_funcs.getOrPutContextAdapted(gpa, {}, adapter, .{ .target = target }); | |
| 5075 | 5105 | if (!gop.found_existing) { |
| 5076 | 5106 | const new_module_func = try gpa.create(Module.Fn); |
| 5077 | 5107 | gop.key_ptr.* = new_module_func; |
| ... | ... | @@ -5255,7 +5285,7 @@ fn instantiateGenericCall( |
| 5255 | 5285 | new_decl.analysis = .complete; |
| 5256 | 5286 | |
| 5257 | 5287 | log.debug("generic function '{s}' instantiated with type {}", .{ |
| 5258 | new_decl.name, new_decl.ty, | |
| 5288 | new_decl.name, new_decl.ty.fmtDebug(), | |
| 5259 | 5289 | }); |
| 5260 | 5290 | |
| 5261 | 5291 | // Queue up a `codegen_func` work item for the new Fn. The `comptime_args` field |
| ... | ... | @@ -5410,7 +5440,8 @@ fn zirArrayType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 5410 | 5440 | const bin_inst = sema.code.instructions.items(.data)[inst].bin; |
| 5411 | 5441 | const len = try sema.resolveInt(block, .unneeded, bin_inst.lhs, Type.usize); |
| 5412 | 5442 | const elem_type = try sema.resolveType(block, .unneeded, bin_inst.rhs); |
| 5413 | const array_ty = try Type.array(sema.arena, len, null, elem_type); | |
| 5443 | const target = sema.mod.getTarget(); | |
| 5444 | const array_ty = try Type.array(sema.arena, len, null, elem_type, target); | |
| 5414 | 5445 | |
| 5415 | 5446 | return sema.addType(array_ty); |
| 5416 | 5447 | } |
| ... | ... | @@ -5429,7 +5460,8 @@ fn zirArrayTypeSentinel(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Compil |
| 5429 | 5460 | const uncasted_sentinel = sema.resolveInst(extra.sentinel); |
| 5430 | 5461 | const sentinel = try sema.coerce(block, elem_type, uncasted_sentinel, sentinel_src); |
| 5431 | 5462 | const sentinel_val = try sema.resolveConstValue(block, sentinel_src, sentinel); |
| 5432 | const array_ty = try Type.array(sema.arena, len, sentinel_val, elem_type); | |
| 5463 | const target = sema.mod.getTarget(); | |
| 5464 | const array_ty = try Type.array(sema.arena, len, sentinel_val, elem_type, target); | |
| 5433 | 5465 | |
| 5434 | 5466 | return sema.addType(array_ty); |
| 5435 | 5467 | } |
| ... | ... | @@ -5456,13 +5488,14 @@ fn zirErrorUnionType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr |
| 5456 | 5488 | const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node }; |
| 5457 | 5489 | const error_set = try sema.resolveType(block, lhs_src, extra.lhs); |
| 5458 | 5490 | const payload = try sema.resolveType(block, rhs_src, extra.rhs); |
| 5491 | const target = sema.mod.getTarget(); | |
| 5459 | 5492 | |
| 5460 | 5493 | if (error_set.zigTypeTag() != .ErrorSet) { |
| 5461 | 5494 | return sema.fail(block, lhs_src, "expected error set type, found {}", .{ |
| 5462 | error_set, | |
| 5495 | error_set.fmt(target), | |
| 5463 | 5496 | }); |
| 5464 | 5497 | } |
| 5465 | const err_union_ty = try Module.errorUnionType(sema.arena, error_set, payload); | |
| 5498 | const err_union_ty = try Type.errorUnion(sema.arena, error_set, payload, target); | |
| 5466 | 5499 | return sema.addType(err_union_ty); |
| 5467 | 5500 | } |
| 5468 | 5501 | |
| ... | ... | @@ -5520,9 +5553,10 @@ fn zirIntToError(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 5520 | 5553 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 5521 | 5554 | |
| 5522 | 5555 | const op = sema.resolveInst(inst_data.operand); |
| 5556 | const target = sema.mod.getTarget(); | |
| 5523 | 5557 | |
| 5524 | 5558 | if (try sema.resolveDefinedValue(block, operand_src, op)) |value| { |
| 5525 | const int = value.toUnsignedInt(); | |
| 5559 | const int = value.toUnsignedInt(target); | |
| 5526 | 5560 | if (int > sema.mod.global_error_set.count() or int == 0) |
| 5527 | 5561 | return sema.fail(block, operand_src, "integer value {d} represents no error", .{int}); |
| 5528 | 5562 | const payload = try sema.arena.create(Value.Payload.Error); |
| ... | ... | @@ -5569,10 +5603,11 @@ fn zirMergeErrorSets(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr |
| 5569 | 5603 | } |
| 5570 | 5604 | const lhs_ty = try sema.analyzeAsType(block, lhs_src, lhs); |
| 5571 | 5605 | const rhs_ty = try sema.analyzeAsType(block, rhs_src, rhs); |
| 5606 | const target = sema.mod.getTarget(); | |
| 5572 | 5607 | if (lhs_ty.zigTypeTag() != .ErrorSet) |
| 5573 | return sema.fail(block, lhs_src, "expected error set type, found {}", .{lhs_ty}); | |
| 5608 | return sema.fail(block, lhs_src, "expected error set type, found {}", .{lhs_ty.fmt(target)}); | |
| 5574 | 5609 | if (rhs_ty.zigTypeTag() != .ErrorSet) |
| 5575 | return sema.fail(block, rhs_src, "expected error set type, found {}", .{rhs_ty}); | |
| 5610 | return sema.fail(block, rhs_src, "expected error set type, found {}", .{rhs_ty.fmt(target)}); | |
| 5576 | 5611 | |
| 5577 | 5612 | // Anything merged with anyerror is anyerror. |
| 5578 | 5613 | if (lhs_ty.tag() == .anyerror or rhs_ty.tag() == .anyerror) { |
| ... | ... | @@ -5618,6 +5653,7 @@ fn zirEnumToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 5618 | 5653 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 5619 | 5654 | const operand = sema.resolveInst(inst_data.operand); |
| 5620 | 5655 | const operand_ty = sema.typeOf(operand); |
| 5656 | const target = sema.mod.getTarget(); | |
| 5621 | 5657 | |
| 5622 | 5658 | const enum_tag: Air.Inst.Ref = switch (operand_ty.zigTypeTag()) { |
| 5623 | 5659 | .Enum => operand, |
| ... | ... | @@ -5634,7 +5670,7 @@ fn zirEnumToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 5634 | 5670 | }, |
| 5635 | 5671 | else => { |
| 5636 | 5672 | return sema.fail(block, operand_src, "expected enum or tagged union, found {}", .{ |
| 5637 | operand_ty, | |
| 5673 | operand_ty.fmt(target), | |
| 5638 | 5674 | }); |
| 5639 | 5675 | }, |
| 5640 | 5676 | }; |
| ... | ... | @@ -5668,7 +5704,7 @@ fn zirIntToEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 5668 | 5704 | const operand = sema.resolveInst(extra.rhs); |
| 5669 | 5705 | |
| 5670 | 5706 | if (dest_ty.zigTypeTag() != .Enum) { |
| 5671 | return sema.fail(block, dest_ty_src, "expected enum, found {}", .{dest_ty}); | |
| 5707 | return sema.fail(block, dest_ty_src, "expected enum, found {}", .{dest_ty.fmt(target)}); | |
| 5672 | 5708 | } |
| 5673 | 5709 | |
| 5674 | 5710 | if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |int_val| { |
| ... | ... | @@ -5684,7 +5720,7 @@ fn zirIntToEnum(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 5684 | 5720 | block, |
| 5685 | 5721 | src, |
| 5686 | 5722 | "enum '{}' has no tag with value {}", |
| 5687 | .{ dest_ty, int_val.fmtValue(sema.typeOf(operand)) }, | |
| 5723 | .{ dest_ty.fmt(target), int_val.fmtValue(sema.typeOf(operand), target) }, | |
| 5688 | 5724 | ); |
| 5689 | 5725 | errdefer msg.destroy(sema.gpa); |
| 5690 | 5726 | try sema.mod.errNoteNonLazy( |
| ... | ... | @@ -5733,13 +5769,13 @@ fn analyzeOptionalPayloadPtr( |
| 5733 | 5769 | const optional_ptr_ty = sema.typeOf(optional_ptr); |
| 5734 | 5770 | assert(optional_ptr_ty.zigTypeTag() == .Pointer); |
| 5735 | 5771 | |
| 5772 | const target = sema.mod.getTarget(); | |
| 5736 | 5773 | const opt_type = optional_ptr_ty.elemType(); |
| 5737 | 5774 | if (opt_type.zigTypeTag() != .Optional) { |
| 5738 | return sema.fail(block, src, "expected optional type, found {}", .{opt_type}); | |
| 5775 | return sema.fail(block, src, "expected optional type, found {}", .{opt_type.fmt(target)}); | |
| 5739 | 5776 | } |
| 5740 | 5777 | |
| 5741 | 5778 | const child_type = try opt_type.optionalChildAlloc(sema.arena); |
| 5742 | const target = sema.mod.getTarget(); | |
| 5743 | 5779 | const child_pointer = try Type.ptr(sema.arena, target, .{ |
| 5744 | 5780 | .pointee_type = child_type, |
| 5745 | 5781 | .mutable = !optional_ptr_ty.isConstPtr(), |
| ... | ... | @@ -5858,8 +5894,12 @@ fn zirErrUnionPayload( |
| 5858 | 5894 | const operand = sema.resolveInst(inst_data.operand); |
| 5859 | 5895 | const operand_src = src; |
| 5860 | 5896 | const operand_ty = sema.typeOf(operand); |
| 5861 | if (operand_ty.zigTypeTag() != .ErrorUnion) | |
| 5862 | return sema.fail(block, operand_src, "expected error union type, found '{}'", .{operand_ty}); | |
| 5897 | if (operand_ty.zigTypeTag() != .ErrorUnion) { | |
| 5898 | const target = sema.mod.getTarget(); | |
| 5899 | return sema.fail(block, operand_src, "expected error union type, found '{}'", .{ | |
| 5900 | operand_ty.fmt(target), | |
| 5901 | }); | |
| 5902 | } | |
| 5863 | 5903 | |
| 5864 | 5904 | if (try sema.resolveDefinedValue(block, src, operand)) |val| { |
| 5865 | 5905 | if (val.getError()) |name| { |
| ... | ... | @@ -5906,11 +5946,14 @@ fn analyzeErrUnionPayloadPtr( |
| 5906 | 5946 | const operand_ty = sema.typeOf(operand); |
| 5907 | 5947 | assert(operand_ty.zigTypeTag() == .Pointer); |
| 5908 | 5948 | |
| 5909 | if (operand_ty.elemType().zigTypeTag() != .ErrorUnion) | |
| 5910 | return sema.fail(block, src, "expected error union type, found {}", .{operand_ty.elemType()}); | |
| 5949 | const target = sema.mod.getTarget(); | |
| 5950 | if (operand_ty.elemType().zigTypeTag() != .ErrorUnion) { | |
| 5951 | return sema.fail(block, src, "expected error union type, found {}", .{ | |
| 5952 | operand_ty.elemType().fmt(target), | |
| 5953 | }); | |
| 5954 | } | |
| 5911 | 5955 | |
| 5912 | 5956 | const payload_ty = operand_ty.elemType().errorUnionPayload(); |
| 5913 | const target = sema.mod.getTarget(); | |
| 5914 | 5957 | const operand_pointer_ty = try Type.ptr(sema.arena, target, .{ |
| 5915 | 5958 | .pointee_type = payload_ty, |
| 5916 | 5959 | .mutable = !operand_ty.isConstPtr(), |
| ... | ... | @@ -5970,8 +6013,12 @@ fn zirErrUnionCode(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro |
| 5970 | 6013 | const src = inst_data.src(); |
| 5971 | 6014 | const operand = sema.resolveInst(inst_data.operand); |
| 5972 | 6015 | const operand_ty = sema.typeOf(operand); |
| 5973 | if (operand_ty.zigTypeTag() != .ErrorUnion) | |
| 5974 | return sema.fail(block, src, "expected error union type, found '{}'", .{operand_ty}); | |
| 6016 | const target = sema.mod.getTarget(); | |
| 6017 | if (operand_ty.zigTypeTag() != .ErrorUnion) { | |
| 6018 | return sema.fail(block, src, "expected error union type, found '{}'", .{ | |
| 6019 | operand_ty.fmt(target), | |
| 6020 | }); | |
| 6021 | } | |
| 5975 | 6022 | |
| 5976 | 6023 | const result_ty = operand_ty.errorUnionSet(); |
| 5977 | 6024 | |
| ... | ... | @@ -5995,8 +6042,12 @@ fn zirErrUnionCodePtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileE |
| 5995 | 6042 | const operand_ty = sema.typeOf(operand); |
| 5996 | 6043 | assert(operand_ty.zigTypeTag() == .Pointer); |
| 5997 | 6044 | |
| 5998 | if (operand_ty.elemType().zigTypeTag() != .ErrorUnion) | |
| 5999 | return sema.fail(block, src, "expected error union type, found {}", .{operand_ty.elemType()}); | |
| 6045 | if (operand_ty.elemType().zigTypeTag() != .ErrorUnion) { | |
| 6046 | const target = sema.mod.getTarget(); | |
| 6047 | return sema.fail(block, src, "expected error union type, found {}", .{ | |
| 6048 | operand_ty.elemType().fmt(target), | |
| 6049 | }); | |
| 6050 | } | |
| 6000 | 6051 | |
| 6001 | 6052 | const result_ty = operand_ty.elemType().errorUnionSet(); |
| 6002 | 6053 | |
| ... | ... | @@ -6019,8 +6070,12 @@ fn zirEnsureErrPayloadVoid(sema: *Sema, block: *Block, inst: Zir.Inst.Index) Com |
| 6019 | 6070 | const src = inst_data.src(); |
| 6020 | 6071 | const operand = sema.resolveInst(inst_data.operand); |
| 6021 | 6072 | const operand_ty = sema.typeOf(operand); |
| 6022 | if (operand_ty.zigTypeTag() != .ErrorUnion) | |
| 6023 | return sema.fail(block, src, "expected error union type, found '{}'", .{operand_ty}); | |
| 6073 | const target = sema.mod.getTarget(); | |
| 6074 | if (operand_ty.zigTypeTag() != .ErrorUnion) { | |
| 6075 | return sema.fail(block, src, "expected error union type, found '{}'", .{ | |
| 6076 | operand_ty.fmt(target), | |
| 6077 | }); | |
| 6078 | } | |
| 6024 | 6079 | if (operand_ty.errorUnionPayload().zigTypeTag() != .Void) { |
| 6025 | 6080 | return sema.fail(block, src, "expression value is ignored", .{}); |
| 6026 | 6081 | } |
| ... | ... | @@ -6205,7 +6260,7 @@ fn funcCommon( |
| 6205 | 6260 | |
| 6206 | 6261 | const fn_ty: Type = fn_ty: { |
| 6207 | 6262 | const alignment: u32 = if (align_val.tag() == .null_value) 0 else a: { |
| 6208 | const alignment = @intCast(u32, align_val.toUnsignedInt()); | |
| 6263 | const alignment = @intCast(u32, align_val.toUnsignedInt(target)); | |
| 6209 | 6264 | if (alignment == target_util.defaultFunctionAlignment(target)) { |
| 6210 | 6265 | break :a 0; |
| 6211 | 6266 | } else { |
| ... | ... | @@ -6494,7 +6549,8 @@ fn zirPtrToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 6494 | 6549 | const ptr = sema.resolveInst(inst_data.operand); |
| 6495 | 6550 | const ptr_ty = sema.typeOf(ptr); |
| 6496 | 6551 | if (!ptr_ty.isPtrAtRuntime()) { |
| 6497 | return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty}); | |
| 6552 | const target = sema.mod.getTarget(); | |
| 6553 | return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty.fmt(target)}); | |
| 6498 | 6554 | } |
| 6499 | 6555 | if (try sema.resolveMaybeUndefVal(block, ptr_src, ptr)) |ptr_val| { |
| 6500 | 6556 | return sema.addConstant(Type.usize, ptr_val); |
| ... | ... | @@ -6652,6 +6708,7 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 6652 | 6708 | const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs); |
| 6653 | 6709 | const operand = sema.resolveInst(extra.rhs); |
| 6654 | 6710 | |
| 6711 | const target = sema.mod.getTarget(); | |
| 6655 | 6712 | const dest_is_comptime_float = switch (dest_ty.zigTypeTag()) { |
| 6656 | 6713 | .ComptimeFloat => true, |
| 6657 | 6714 | .Float => false, |
| ... | ... | @@ -6659,7 +6716,7 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 6659 | 6716 | block, |
| 6660 | 6717 | dest_ty_src, |
| 6661 | 6718 | "expected float type, found '{}'", |
| 6662 | .{dest_ty}, | |
| 6719 | .{dest_ty.fmt(target)}, | |
| 6663 | 6720 | ), |
| 6664 | 6721 | }; |
| 6665 | 6722 | |
| ... | ... | @@ -6670,7 +6727,7 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 6670 | 6727 | block, |
| 6671 | 6728 | operand_src, |
| 6672 | 6729 | "expected float type, found '{}'", |
| 6673 | .{operand_ty}, | |
| 6730 | .{operand_ty.fmt(target)}, | |
| 6674 | 6731 | ), |
| 6675 | 6732 | } |
| 6676 | 6733 | |
| ... | ... | @@ -6680,7 +6737,6 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 6680 | 6737 | if (dest_is_comptime_float) { |
| 6681 | 6738 | return sema.fail(block, src, "unable to cast runtime value to 'comptime_float'", .{}); |
| 6682 | 6739 | } |
| 6683 | const target = sema.mod.getTarget(); | |
| 6684 | 6740 | const src_bits = operand_ty.floatBits(target); |
| 6685 | 6741 | const dst_bits = dest_ty.floatBits(target); |
| 6686 | 6742 | if (dst_bits >= src_bits) { |
| ... | ... | @@ -6839,13 +6895,14 @@ fn zirSwitchCapture( |
| 6839 | 6895 | const item = sema.resolveInst(scalar_prong.item); |
| 6840 | 6896 | // Previous switch validation ensured this will succeed |
| 6841 | 6897 | const item_val = sema.resolveConstValue(block, .unneeded, item) catch unreachable; |
| 6898 | const target = sema.mod.getTarget(); | |
| 6842 | 6899 | |
| 6843 | 6900 | switch (operand_ty.zigTypeTag()) { |
| 6844 | 6901 | .Union => { |
| 6845 | 6902 | const union_obj = operand_ty.cast(Type.Payload.Union).?.data; |
| 6846 | 6903 | const enum_ty = union_obj.tag_ty; |
| 6847 | 6904 | |
| 6848 | const field_index_usize = enum_ty.enumTagFieldIndex(item_val).?; | |
| 6905 | const field_index_usize = enum_ty.enumTagFieldIndex(item_val, target).?; | |
| 6849 | 6906 | const field_index = @intCast(u32, field_index_usize); |
| 6850 | 6907 | const field = union_obj.fields.values()[field_index]; |
| 6851 | 6908 | |
| ... | ... | @@ -6854,7 +6911,6 @@ fn zirSwitchCapture( |
| 6854 | 6911 | if (is_ref) { |
| 6855 | 6912 | assert(operand_is_ref); |
| 6856 | 6913 | |
| 6857 | const target = sema.mod.getTarget(); | |
| 6858 | 6914 | const field_ty_ptr = try Type.ptr(sema.arena, target, .{ |
| 6859 | 6915 | .pointee_type = field.ty, |
| 6860 | 6916 | .@"addrspace" = .generic, |
| ... | ... | @@ -6894,7 +6950,7 @@ fn zirSwitchCapture( |
| 6894 | 6950 | }, |
| 6895 | 6951 | else => { |
| 6896 | 6952 | return sema.fail(block, operand_src, "switch on type '{}' provides no capture value", .{ |
| 6897 | operand_ty, | |
| 6953 | operand_ty.fmt(target), | |
| 6898 | 6954 | }); |
| 6899 | 6955 | }, |
| 6900 | 6956 | } |
| ... | ... | @@ -6915,6 +6971,7 @@ fn zirSwitchCond( |
| 6915 | 6971 | else |
| 6916 | 6972 | operand_ptr; |
| 6917 | 6973 | const operand_ty = sema.typeOf(operand); |
| 6974 | const target = sema.mod.getTarget(); | |
| 6918 | 6975 | |
| 6919 | 6976 | switch (operand_ty.zigTypeTag()) { |
| 6920 | 6977 | .Type, |
| ... | ... | @@ -6962,7 +7019,7 @@ fn zirSwitchCond( |
| 6962 | 7019 | .Vector, |
| 6963 | 7020 | .Frame, |
| 6964 | 7021 | .AnyFrame, |
| 6965 | => return sema.fail(block, src, "switch on type '{}'", .{operand_ty}), | |
| 7022 | => return sema.fail(block, src, "switch on type '{}'", .{operand_ty.fmt(target)}), | |
| 6966 | 7023 | } |
| 6967 | 7024 | } |
| 6968 | 7025 | |
| ... | ... | @@ -7030,6 +7087,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 7030 | 7087 | return sema.failWithOwnedErrorMsg(block, msg); |
| 7031 | 7088 | } |
| 7032 | 7089 | |
| 7090 | const target = sema.mod.getTarget(); | |
| 7091 | ||
| 7033 | 7092 | // Validate for duplicate items, missing else prong, and invalid range. |
| 7034 | 7093 | switch (operand_ty.zigTypeTag()) { |
| 7035 | 7094 | .Enum => { |
| ... | ... | @@ -7115,7 +7174,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 7115 | 7174 | operand_ty.declSrcLoc(), |
| 7116 | 7175 | msg, |
| 7117 | 7176 | "enum '{}' declared here", |
| 7118 | .{operand_ty}, | |
| 7177 | .{operand_ty.fmt(target)}, | |
| 7119 | 7178 | ); |
| 7120 | 7179 | break :msg msg; |
| 7121 | 7180 | }; |
| ... | ... | @@ -7232,7 +7291,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 7232 | 7291 | operand_ty.declSrcLoc(), |
| 7233 | 7292 | msg, |
| 7234 | 7293 | "error set '{}' declared here", |
| 7235 | .{operand_ty}, | |
| 7294 | .{operand_ty.fmt(target)}, | |
| 7236 | 7295 | ); |
| 7237 | 7296 | return sema.failWithOwnedErrorMsg(block, msg); |
| 7238 | 7297 | } |
| ... | ... | @@ -7260,7 +7319,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 7260 | 7319 | }, |
| 7261 | 7320 | .Union => return sema.fail(block, src, "TODO validate switch .Union", .{}), |
| 7262 | 7321 | .Int, .ComptimeInt => { |
| 7263 | var range_set = RangeSet.init(gpa); | |
| 7322 | var range_set = RangeSet.init(gpa, target); | |
| 7264 | 7323 | defer range_set.deinit(); |
| 7265 | 7324 | |
| 7266 | 7325 | var extra_index: usize = special.end; |
| ... | ... | @@ -7333,7 +7392,6 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 7333 | 7392 | var arena = std.heap.ArenaAllocator.init(gpa); |
| 7334 | 7393 | defer arena.deinit(); |
| 7335 | 7394 | |
| 7336 | const target = sema.mod.getTarget(); | |
| 7337 | 7395 | const min_int = try operand_ty.minInt(arena.allocator(), target); |
| 7338 | 7396 | const max_int = try operand_ty.maxInt(arena.allocator(), target); |
| 7339 | 7397 | if (try range_set.spans(min_int, max_int, operand_ty)) { |
| ... | ... | @@ -7437,11 +7495,14 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 7437 | 7495 | block, |
| 7438 | 7496 | src, |
| 7439 | 7497 | "else prong required when switching on type '{}'", |
| 7440 | .{operand_ty}, | |
| 7498 | .{operand_ty.fmt(target)}, | |
| 7441 | 7499 | ); |
| 7442 | 7500 | } |
| 7443 | 7501 | |
| 7444 | var seen_values = ValueSrcMap.initContext(gpa, .{ .ty = operand_ty }); | |
| 7502 | var seen_values = ValueSrcMap.initContext(gpa, .{ | |
| 7503 | .ty = operand_ty, | |
| 7504 | .target = target, | |
| 7505 | }); | |
| 7445 | 7506 | defer seen_values.deinit(); |
| 7446 | 7507 | |
| 7447 | 7508 | var extra_index: usize = special.end; |
| ... | ... | @@ -7505,7 +7566,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 7505 | 7566 | .ComptimeFloat, |
| 7506 | 7567 | .Float, |
| 7507 | 7568 | => return sema.fail(block, operand_src, "invalid switch operand type '{}'", .{ |
| 7508 | operand_ty, | |
| 7569 | operand_ty.fmt(target), | |
| 7509 | 7570 | }), |
| 7510 | 7571 | } |
| 7511 | 7572 | |
| ... | ... | @@ -7555,7 +7616,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 7555 | 7616 | const item = sema.resolveInst(item_ref); |
| 7556 | 7617 | // Validation above ensured these will succeed. |
| 7557 | 7618 | const item_val = sema.resolveConstValue(&child_block, .unneeded, item) catch unreachable; |
| 7558 | if (operand_val.eql(item_val, operand_ty)) { | |
| 7619 | if (operand_val.eql(item_val, operand_ty, target)) { | |
| 7559 | 7620 | return sema.resolveBlockBody(block, src, &child_block, body, inst, merges); |
| 7560 | 7621 | } |
| 7561 | 7622 | } |
| ... | ... | @@ -7577,7 +7638,7 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 7577 | 7638 | const item = sema.resolveInst(item_ref); |
| 7578 | 7639 | // Validation above ensured these will succeed. |
| 7579 | 7640 | const item_val = sema.resolveConstValue(&child_block, .unneeded, item) catch unreachable; |
| 7580 | if (operand_val.eql(item_val, operand_ty)) { | |
| 7641 | if (operand_val.eql(item_val, operand_ty, target)) { | |
| 7581 | 7642 | return sema.resolveBlockBody(block, src, &child_block, body, inst, merges); |
| 7582 | 7643 | } |
| 7583 | 7644 | } |
| ... | ... | @@ -7592,8 +7653,8 @@ fn zirSwitchBlock(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 7592 | 7653 | // Validation above ensured these will succeed. |
| 7593 | 7654 | const first_tv = sema.resolveInstConst(&child_block, .unneeded, item_first) catch unreachable; |
| 7594 | 7655 | const last_tv = sema.resolveInstConst(&child_block, .unneeded, item_last) catch unreachable; |
| 7595 | if (Value.compare(operand_val, .gte, first_tv.val, operand_ty) and | |
| 7596 | Value.compare(operand_val, .lte, last_tv.val, operand_ty)) | |
| 7656 | if (Value.compare(operand_val, .gte, first_tv.val, operand_ty, target) and | |
| 7657 | Value.compare(operand_val, .lte, last_tv.val, operand_ty, target)) | |
| 7597 | 7658 | { |
| 7598 | 7659 | return sema.resolveBlockBody(block, src, &child_block, body, inst, merges); |
| 7599 | 7660 | } |
| ... | ... | @@ -7907,14 +7968,15 @@ fn validateSwitchItemEnum( |
| 7907 | 7968 | switch_prong_src: Module.SwitchProngSrc, |
| 7908 | 7969 | ) CompileError!void { |
| 7909 | 7970 | const item_tv = try sema.resolveSwitchItemVal(block, item_ref, src_node_offset, switch_prong_src, .none); |
| 7910 | const field_index = item_tv.ty.enumTagFieldIndex(item_tv.val) orelse { | |
| 7971 | const target = sema.mod.getTarget(); | |
| 7972 | const field_index = item_tv.ty.enumTagFieldIndex(item_tv.val, target) orelse { | |
| 7911 | 7973 | const msg = msg: { |
| 7912 | 7974 | const src = switch_prong_src.resolve(sema.gpa, block.src_decl, src_node_offset, .none); |
| 7913 | 7975 | const msg = try sema.errMsg( |
| 7914 | 7976 | block, |
| 7915 | 7977 | src, |
| 7916 | 7978 | "enum '{}' has no tag with value '{}'", |
| 7917 | .{ item_tv.ty, item_tv.val.fmtValue(item_tv.ty) }, | |
| 7979 | .{ item_tv.ty.fmt(target), item_tv.val.fmtValue(item_tv.ty, target) }, | |
| 7918 | 7980 | ); |
| 7919 | 7981 | errdefer msg.destroy(sema.gpa); |
| 7920 | 7982 | try sema.mod.errNoteNonLazy( |
| ... | ... | @@ -8030,12 +8092,13 @@ fn validateSwitchNoRange( |
| 8030 | 8092 | const operand_src: LazySrcLoc = .{ .node_offset_switch_operand = src_node_offset }; |
| 8031 | 8093 | const range_src: LazySrcLoc = .{ .node_offset_switch_range = src_node_offset }; |
| 8032 | 8094 | |
| 8095 | const target = sema.mod.getTarget(); | |
| 8033 | 8096 | const msg = msg: { |
| 8034 | 8097 | const msg = try sema.errMsg( |
| 8035 | 8098 | block, |
| 8036 | 8099 | operand_src, |
| 8037 | 8100 | "ranges not allowed when switching on type '{}'", |
| 8038 | .{operand_ty}, | |
| 8101 | .{operand_ty.fmt(target)}, | |
| 8039 | 8102 | ); |
| 8040 | 8103 | errdefer msg.destroy(sema.gpa); |
| 8041 | 8104 | try sema.errNote( |
| ... | ... | @@ -8058,6 +8121,7 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 8058 | 8121 | const unresolved_ty = try sema.resolveType(block, ty_src, extra.lhs); |
| 8059 | 8122 | const field_name = try sema.resolveConstString(block, name_src, extra.rhs); |
| 8060 | 8123 | const ty = try sema.resolveTypeFields(block, ty_src, unresolved_ty); |
| 8124 | const target = sema.mod.getTarget(); | |
| 8061 | 8125 | |
| 8062 | 8126 | const has_field = hf: { |
| 8063 | 8127 | if (ty.isSlice()) { |
| ... | ... | @@ -8080,7 +8144,7 @@ fn zirHasField(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 8080 | 8144 | .Enum => ty.enumFields().contains(field_name), |
| 8081 | 8145 | .Array => mem.eql(u8, field_name, "len"), |
| 8082 | 8146 | else => return sema.fail(block, ty_src, "type '{}' does not support '@hasField'", .{ |
| 8083 | ty, | |
| 8147 | ty.fmt(target), | |
| 8084 | 8148 | }), |
| 8085 | 8149 | }; |
| 8086 | 8150 | }; |
| ... | ... | @@ -8227,25 +8291,25 @@ fn zirShl( |
| 8227 | 8291 | |
| 8228 | 8292 | const val = switch (air_tag) { |
| 8229 | 8293 | .shl_exact => val: { |
| 8230 | const shifted = try lhs_val.shl(rhs_val, lhs_ty, sema.arena); | |
| 8294 | const shifted = try lhs_val.shl(rhs_val, lhs_ty, sema.arena, target); | |
| 8231 | 8295 | if (scalar_ty.zigTypeTag() == .ComptimeInt) { |
| 8232 | 8296 | break :val shifted; |
| 8233 | 8297 | } |
| 8234 | 8298 | const int_info = scalar_ty.intInfo(target); |
| 8235 | const truncated = try shifted.intTrunc(lhs_ty, sema.arena, int_info.signedness, int_info.bits); | |
| 8236 | if (truncated.compare(.eq, shifted, lhs_ty)) { | |
| 8299 | const truncated = try shifted.intTrunc(lhs_ty, sema.arena, int_info.signedness, int_info.bits, target); | |
| 8300 | if (truncated.compare(.eq, shifted, lhs_ty, target)) { | |
| 8237 | 8301 | break :val shifted; |
| 8238 | 8302 | } |
| 8239 | 8303 | return sema.addConstUndef(lhs_ty); |
| 8240 | 8304 | }, |
| 8241 | 8305 | |
| 8242 | 8306 | .shl_sat => if (scalar_ty.zigTypeTag() == .ComptimeInt) |
| 8243 | try lhs_val.shl(rhs_val, lhs_ty, sema.arena) | |
| 8307 | try lhs_val.shl(rhs_val, lhs_ty, sema.arena, target) | |
| 8244 | 8308 | else |
| 8245 | 8309 | try lhs_val.shlSat(rhs_val, lhs_ty, sema.arena, target), |
| 8246 | 8310 | |
| 8247 | 8311 | .shl => if (scalar_ty.zigTypeTag() == .ComptimeInt) |
| 8248 | try lhs_val.shl(rhs_val, lhs_ty, sema.arena) | |
| 8312 | try lhs_val.shl(rhs_val, lhs_ty, sema.arena, target) | |
| 8249 | 8313 | else |
| 8250 | 8314 | try lhs_val.shlTrunc(rhs_val, lhs_ty, sema.arena, target), |
| 8251 | 8315 | |
| ... | ... | @@ -8296,6 +8360,7 @@ fn zirShr( |
| 8296 | 8360 | const lhs_ty = sema.typeOf(lhs); |
| 8297 | 8361 | const rhs_ty = sema.typeOf(rhs); |
| 8298 | 8362 | try sema.checkVectorizableBinaryOperands(block, src, lhs_ty, rhs_ty, lhs_src, rhs_src); |
| 8363 | const target = sema.mod.getTarget(); | |
| 8299 | 8364 | |
| 8300 | 8365 | const runtime_src = if (try sema.resolveMaybeUndefVal(block, rhs_src, rhs)) |rhs_val| rs: { |
| 8301 | 8366 | if (try sema.resolveMaybeUndefVal(block, lhs_src, lhs)) |lhs_val| { |
| ... | ... | @@ -8308,12 +8373,12 @@ fn zirShr( |
| 8308 | 8373 | } |
| 8309 | 8374 | if (air_tag == .shr_exact) { |
| 8310 | 8375 | // Detect if any ones would be shifted out. |
| 8311 | const truncated = try lhs_val.intTruncBitsAsValue(lhs_ty, sema.arena, .unsigned, rhs_val); | |
| 8376 | const truncated = try lhs_val.intTruncBitsAsValue(lhs_ty, sema.arena, .unsigned, rhs_val, target); | |
| 8312 | 8377 | if (!truncated.compareWithZero(.eq)) { |
| 8313 | 8378 | return sema.addConstUndef(lhs_ty); |
| 8314 | 8379 | } |
| 8315 | 8380 | } |
| 8316 | const val = try lhs_val.shr(rhs_val, lhs_ty, sema.arena); | |
| 8381 | const val = try lhs_val.shr(rhs_val, lhs_ty, sema.arena, target); | |
| 8317 | 8382 | return sema.addConstant(lhs_ty, val); |
| 8318 | 8383 | } else { |
| 8319 | 8384 | // Even if lhs is not comptime known, we can still deduce certain things based |
| ... | ... | @@ -8359,6 +8424,7 @@ fn zirBitwise( |
| 8359 | 8424 | const casted_rhs = try sema.coerce(block, resolved_type, rhs, rhs_src); |
| 8360 | 8425 | |
| 8361 | 8426 | const is_int = scalar_tag == .Int or scalar_tag == .ComptimeInt; |
| 8427 | const target = sema.mod.getTarget(); | |
| 8362 | 8428 | |
| 8363 | 8429 | if (!is_int) { |
| 8364 | 8430 | return sema.fail(block, src, "invalid operands to binary bitwise expression: '{s}' and '{s}'", .{ @tagName(lhs_ty.zigTypeTag()), @tagName(rhs_ty.zigTypeTag()) }); |
| ... | ... | @@ -8367,9 +8433,9 @@ fn zirBitwise( |
| 8367 | 8433 | if (try sema.resolveMaybeUndefVal(block, lhs_src, casted_lhs)) |lhs_val| { |
| 8368 | 8434 | if (try sema.resolveMaybeUndefVal(block, rhs_src, casted_rhs)) |rhs_val| { |
| 8369 | 8435 | const result_val = switch (air_tag) { |
| 8370 | .bit_and => try lhs_val.bitwiseAnd(rhs_val, resolved_type, sema.arena), | |
| 8371 | .bit_or => try lhs_val.bitwiseOr(rhs_val, resolved_type, sema.arena), | |
| 8372 | .xor => try lhs_val.bitwiseXor(rhs_val, resolved_type, sema.arena), | |
| 8436 | .bit_and => try lhs_val.bitwiseAnd(rhs_val, resolved_type, sema.arena, target), | |
| 8437 | .bit_or => try lhs_val.bitwiseOr(rhs_val, resolved_type, sema.arena, target), | |
| 8438 | .xor => try lhs_val.bitwiseXor(rhs_val, resolved_type, sema.arena, target), | |
| 8373 | 8439 | else => unreachable, |
| 8374 | 8440 | }; |
| 8375 | 8441 | return sema.addConstant(resolved_type, result_val); |
| ... | ... | @@ -8391,13 +8457,15 @@ fn zirBitNot(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 8391 | 8457 | const operand = sema.resolveInst(inst_data.operand); |
| 8392 | 8458 | const operand_type = sema.typeOf(operand); |
| 8393 | 8459 | const scalar_type = operand_type.scalarType(); |
| 8460 | const target = sema.mod.getTarget(); | |
| 8394 | 8461 | |
| 8395 | 8462 | if (scalar_type.zigTypeTag() != .Int) { |
| 8396 | return sema.fail(block, src, "unable to perform binary not operation on type '{}'", .{operand_type}); | |
| 8463 | return sema.fail(block, src, "unable to perform binary not operation on type '{}'", .{ | |
| 8464 | operand_type.fmt(target), | |
| 8465 | }); | |
| 8397 | 8466 | } |
| 8398 | 8467 | |
| 8399 | 8468 | if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |val| { |
| 8400 | const target = sema.mod.getTarget(); | |
| 8401 | 8469 | if (val.isUndef()) { |
| 8402 | 8470 | return sema.addConstUndef(operand_type); |
| 8403 | 8471 | } else if (operand_type.zigTypeTag() == .Vector) { |
| ... | ... | @@ -8513,19 +8581,22 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 8513 | 8581 | const lhs_src: LazySrcLoc = .{ .node_offset_bin_lhs = inst_data.src_node }; |
| 8514 | 8582 | const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node }; |
| 8515 | 8583 | |
| 8584 | const target = sema.mod.getTarget(); | |
| 8516 | 8585 | const lhs_info = (try sema.getArrayCatInfo(block, lhs_src, lhs)) orelse |
| 8517 | return sema.fail(block, lhs_src, "expected array, found '{}'", .{lhs_ty}); | |
| 8586 | return sema.fail(block, lhs_src, "expected array, found '{}'", .{lhs_ty.fmt(target)}); | |
| 8518 | 8587 | const rhs_info = (try sema.getArrayCatInfo(block, rhs_src, rhs)) orelse |
| 8519 | return sema.fail(block, rhs_src, "expected array, found '{}'", .{rhs_ty}); | |
| 8520 | if (!lhs_info.elem_type.eql(rhs_info.elem_type)) { | |
| 8521 | return sema.fail(block, rhs_src, "expected array of type '{}', found '{}'", .{ lhs_info.elem_type, rhs_ty }); | |
| 8588 | return sema.fail(block, rhs_src, "expected array, found '{}'", .{rhs_ty.fmt(target)}); | |
| 8589 | if (!lhs_info.elem_type.eql(rhs_info.elem_type, target)) { | |
| 8590 | return sema.fail(block, rhs_src, "expected array of type '{}', found '{}'", .{ | |
| 8591 | lhs_info.elem_type.fmt(target), rhs_ty.fmt(target), | |
| 8592 | }); | |
| 8522 | 8593 | } |
| 8523 | 8594 | |
| 8524 | 8595 | // When there is a sentinel mismatch, no sentinel on the result. The type system |
| 8525 | 8596 | // will catch this if it is a problem. |
| 8526 | 8597 | var res_sent: ?Value = null; |
| 8527 | 8598 | if (rhs_info.sentinel != null and lhs_info.sentinel != null) { |
| 8528 | if (rhs_info.sentinel.?.eql(lhs_info.sentinel.?, lhs_info.elem_type)) { | |
| 8599 | if (rhs_info.sentinel.?.eql(lhs_info.sentinel.?, lhs_info.elem_type, target)) { | |
| 8529 | 8600 | res_sent = lhs_info.sentinel.?; |
| 8530 | 8601 | } |
| 8531 | 8602 | } |
| ... | ... | @@ -8586,6 +8657,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 8586 | 8657 | |
| 8587 | 8658 | fn getArrayCatInfo(sema: *Sema, block: *Block, src: LazySrcLoc, inst: Air.Inst.Ref) !?Type.ArrayInfo { |
| 8588 | 8659 | const t = sema.typeOf(inst); |
| 8660 | const target = sema.mod.getTarget(); | |
| 8589 | 8661 | return switch (t.zigTypeTag()) { |
| 8590 | 8662 | .Array => t.arrayInfo(), |
| 8591 | 8663 | .Pointer => blk: { |
| ... | ... | @@ -8595,7 +8667,7 @@ fn getArrayCatInfo(sema: *Sema, block: *Block, src: LazySrcLoc, inst: Air.Inst.R |
| 8595 | 8667 | return Type.ArrayInfo{ |
| 8596 | 8668 | .elem_type = t.childType(), |
| 8597 | 8669 | .sentinel = t.sentinel(), |
| 8598 | .len = val.sliceLen(), | |
| 8670 | .len = val.sliceLen(target), | |
| 8599 | 8671 | }; |
| 8600 | 8672 | } |
| 8601 | 8673 | if (ptrinfo.pointee_type.zigTypeTag() != .Array) return null; |
| ... | ... | @@ -8691,9 +8763,10 @@ fn zirArrayMul(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 8691 | 8763 | if (lhs_ty.isTuple()) { |
| 8692 | 8764 | return sema.analyzeTupleMul(block, inst_data.src_node, lhs, factor); |
| 8693 | 8765 | } |
| 8766 | const target = sema.mod.getTarget(); | |
| 8694 | 8767 | |
| 8695 | 8768 | const mulinfo = (try sema.getArrayCatInfo(block, lhs_src, lhs)) orelse |
| 8696 | return sema.fail(block, lhs_src, "expected array, found '{}'", .{lhs_ty}); | |
| 8769 | return sema.fail(block, lhs_src, "expected array, found '{}'", .{lhs_ty.fmt(target)}); | |
| 8697 | 8770 | |
| 8698 | 8771 | const final_len_u64 = std.math.mul(u64, mulinfo.len, factor) catch |
| 8699 | 8772 | return sema.fail(block, rhs_src, "operation results in overflow", .{}); |
| ... | ... | @@ -8771,8 +8844,9 @@ fn zirNegate( |
| 8771 | 8844 | const rhs_ty = sema.typeOf(rhs); |
| 8772 | 8845 | const rhs_scalar_ty = rhs_ty.scalarType(); |
| 8773 | 8846 | |
| 8847 | const target = sema.mod.getTarget(); | |
| 8774 | 8848 | if (tag_override == .sub and rhs_scalar_ty.isUnsignedInt()) { |
| 8775 | return sema.fail(block, src, "negation of type '{}'", .{rhs_ty}); | |
| 8849 | return sema.fail(block, src, "negation of type '{}'", .{rhs_ty.fmt(target)}); | |
| 8776 | 8850 | } |
| 8777 | 8851 | |
| 8778 | 8852 | const lhs = if (rhs_ty.zigTypeTag() == .Vector) |
| ... | ... | @@ -8824,15 +8898,14 @@ fn zirOverflowArithmetic( |
| 8824 | 8898 | const ptr = sema.resolveInst(extra.ptr); |
| 8825 | 8899 | |
| 8826 | 8900 | const lhs_ty = sema.typeOf(lhs); |
| 8901 | const target = sema.mod.getTarget(); | |
| 8827 | 8902 | |
| 8828 | 8903 | // Note, the types of lhs/rhs (also for shifting)/ptr are already correct as ensured by astgen. |
| 8829 | 8904 | const dest_ty = lhs_ty; |
| 8830 | 8905 | if (dest_ty.zigTypeTag() != .Int) { |
| 8831 | return sema.fail(block, src, "expected integer type, found '{}'", .{dest_ty}); | |
| 8906 | return sema.fail(block, src, "expected integer type, found '{}'", .{dest_ty.fmt(target)}); | |
| 8832 | 8907 | } |
| 8833 | 8908 | |
| 8834 | const target = sema.mod.getTarget(); | |
| 8835 | ||
| 8836 | 8909 | const maybe_lhs_val = try sema.resolveMaybeUndefVal(block, lhs_src, lhs); |
| 8837 | 8910 | const maybe_rhs_val = try sema.resolveMaybeUndefVal(block, rhs_src, rhs); |
| 8838 | 8911 | |
| ... | ... | @@ -8894,7 +8967,7 @@ fn zirOverflowArithmetic( |
| 8894 | 8967 | if (!lhs_val.isUndef()) { |
| 8895 | 8968 | if (lhs_val.compareWithZero(.eq)) { |
| 8896 | 8969 | break :result .{ .overflowed = .no, .wrapped = lhs }; |
| 8897 | } else if (lhs_val.compare(.eq, Value.one, dest_ty)) { | |
| 8970 | } else if (lhs_val.compare(.eq, Value.one, dest_ty, target)) { | |
| 8898 | 8971 | break :result .{ .overflowed = .no, .wrapped = rhs }; |
| 8899 | 8972 | } |
| 8900 | 8973 | } |
| ... | ... | @@ -8904,7 +8977,7 @@ fn zirOverflowArithmetic( |
| 8904 | 8977 | if (!rhs_val.isUndef()) { |
| 8905 | 8978 | if (rhs_val.compareWithZero(.eq)) { |
| 8906 | 8979 | break :result .{ .overflowed = .no, .wrapped = rhs }; |
| 8907 | } else if (rhs_val.compare(.eq, Value.one, dest_ty)) { | |
| 8980 | } else if (rhs_val.compare(.eq, Value.one, dest_ty, target)) { | |
| 8908 | 8981 | break :result .{ .overflowed = .no, .wrapped = lhs }; |
| 8909 | 8982 | } |
| 8910 | 8983 | } |
| ... | ... | @@ -9079,7 +9152,7 @@ fn analyzeArithmetic( |
| 9079 | 9152 | if (is_int) { |
| 9080 | 9153 | return sema.addConstant( |
| 9081 | 9154 | resolved_type, |
| 9082 | try lhs_val.intAdd(rhs_val, resolved_type, sema.arena), | |
| 9155 | try lhs_val.intAdd(rhs_val, resolved_type, sema.arena, target), | |
| 9083 | 9156 | ); |
| 9084 | 9157 | } else { |
| 9085 | 9158 | return sema.addConstant( |
| ... | ... | @@ -9132,7 +9205,7 @@ fn analyzeArithmetic( |
| 9132 | 9205 | } |
| 9133 | 9206 | if (maybe_lhs_val) |lhs_val| { |
| 9134 | 9207 | const val = if (scalar_tag == .ComptimeInt) |
| 9135 | try lhs_val.intAdd(rhs_val, resolved_type, sema.arena) | |
| 9208 | try lhs_val.intAdd(rhs_val, resolved_type, sema.arena, target) | |
| 9136 | 9209 | else |
| 9137 | 9210 | try lhs_val.intAddSat(rhs_val, resolved_type, sema.arena, target); |
| 9138 | 9211 | |
| ... | ... | @@ -9172,7 +9245,7 @@ fn analyzeArithmetic( |
| 9172 | 9245 | if (is_int) { |
| 9173 | 9246 | return sema.addConstant( |
| 9174 | 9247 | resolved_type, |
| 9175 | try lhs_val.intSub(rhs_val, resolved_type, sema.arena), | |
| 9248 | try lhs_val.intSub(rhs_val, resolved_type, sema.arena, target), | |
| 9176 | 9249 | ); |
| 9177 | 9250 | } else { |
| 9178 | 9251 | return sema.addConstant( |
| ... | ... | @@ -9225,7 +9298,7 @@ fn analyzeArithmetic( |
| 9225 | 9298 | } |
| 9226 | 9299 | if (maybe_rhs_val) |rhs_val| { |
| 9227 | 9300 | const val = if (scalar_tag == .ComptimeInt) |
| 9228 | try lhs_val.intSub(rhs_val, resolved_type, sema.arena) | |
| 9301 | try lhs_val.intSub(rhs_val, resolved_type, sema.arena, target) | |
| 9229 | 9302 | else |
| 9230 | 9303 | try lhs_val.intSubSat(rhs_val, resolved_type, sema.arena, target); |
| 9231 | 9304 | |
| ... | ... | @@ -9275,7 +9348,7 @@ fn analyzeArithmetic( |
| 9275 | 9348 | if (lhs_val.isUndef()) { |
| 9276 | 9349 | if (lhs_scalar_ty.isSignedInt() and rhs_scalar_ty.isSignedInt()) { |
| 9277 | 9350 | if (maybe_rhs_val) |rhs_val| { |
| 9278 | if (rhs_val.compare(.neq, Value.negative_one, rhs_ty)) { | |
| 9351 | if (rhs_val.compare(.neq, Value.negative_one, rhs_ty, target)) { | |
| 9279 | 9352 | return sema.addConstUndef(resolved_type); |
| 9280 | 9353 | } |
| 9281 | 9354 | } |
| ... | ... | @@ -9288,7 +9361,7 @@ fn analyzeArithmetic( |
| 9288 | 9361 | if (is_int) { |
| 9289 | 9362 | return sema.addConstant( |
| 9290 | 9363 | resolved_type, |
| 9291 | try lhs_val.intDiv(rhs_val, resolved_type, sema.arena), | |
| 9364 | try lhs_val.intDiv(rhs_val, resolved_type, sema.arena, target), | |
| 9292 | 9365 | ); |
| 9293 | 9366 | } else { |
| 9294 | 9367 | return sema.addConstant( |
| ... | ... | @@ -9350,7 +9423,7 @@ fn analyzeArithmetic( |
| 9350 | 9423 | if (lhs_val.isUndef()) { |
| 9351 | 9424 | if (lhs_scalar_ty.isSignedInt() and rhs_scalar_ty.isSignedInt()) { |
| 9352 | 9425 | if (maybe_rhs_val) |rhs_val| { |
| 9353 | if (rhs_val.compare(.neq, Value.negative_one, rhs_ty)) { | |
| 9426 | if (rhs_val.compare(.neq, Value.negative_one, rhs_ty, target)) { | |
| 9354 | 9427 | return sema.addConstUndef(resolved_type); |
| 9355 | 9428 | } |
| 9356 | 9429 | } |
| ... | ... | @@ -9363,7 +9436,7 @@ fn analyzeArithmetic( |
| 9363 | 9436 | if (is_int) { |
| 9364 | 9437 | return sema.addConstant( |
| 9365 | 9438 | resolved_type, |
| 9366 | try lhs_val.intDiv(rhs_val, resolved_type, sema.arena), | |
| 9439 | try lhs_val.intDiv(rhs_val, resolved_type, sema.arena, target), | |
| 9367 | 9440 | ); |
| 9368 | 9441 | } else { |
| 9369 | 9442 | return sema.addConstant( |
| ... | ... | @@ -9413,7 +9486,7 @@ fn analyzeArithmetic( |
| 9413 | 9486 | if (lhs_val.isUndef()) { |
| 9414 | 9487 | if (lhs_scalar_ty.isSignedInt() and rhs_scalar_ty.isSignedInt()) { |
| 9415 | 9488 | if (maybe_rhs_val) |rhs_val| { |
| 9416 | if (rhs_val.compare(.neq, Value.negative_one, rhs_ty)) { | |
| 9489 | if (rhs_val.compare(.neq, Value.negative_one, rhs_ty, target)) { | |
| 9417 | 9490 | return sema.addConstUndef(resolved_type); |
| 9418 | 9491 | } |
| 9419 | 9492 | } |
| ... | ... | @@ -9426,7 +9499,7 @@ fn analyzeArithmetic( |
| 9426 | 9499 | if (is_int) { |
| 9427 | 9500 | return sema.addConstant( |
| 9428 | 9501 | resolved_type, |
| 9429 | try lhs_val.intDivFloor(rhs_val, resolved_type, sema.arena), | |
| 9502 | try lhs_val.intDivFloor(rhs_val, resolved_type, sema.arena, target), | |
| 9430 | 9503 | ); |
| 9431 | 9504 | } else { |
| 9432 | 9505 | return sema.addConstant( |
| ... | ... | @@ -9477,7 +9550,7 @@ fn analyzeArithmetic( |
| 9477 | 9550 | // TODO: emit compile error if there is a remainder |
| 9478 | 9551 | return sema.addConstant( |
| 9479 | 9552 | resolved_type, |
| 9480 | try lhs_val.intDiv(rhs_val, resolved_type, sema.arena), | |
| 9553 | try lhs_val.intDiv(rhs_val, resolved_type, sema.arena, target), | |
| 9481 | 9554 | ); |
| 9482 | 9555 | } else { |
| 9483 | 9556 | // TODO: emit compile error if there is a remainder |
| ... | ... | @@ -9503,7 +9576,7 @@ fn analyzeArithmetic( |
| 9503 | 9576 | if (lhs_val.compareWithZero(.eq)) { |
| 9504 | 9577 | return sema.addConstant(resolved_type, Value.zero); |
| 9505 | 9578 | } |
| 9506 | if (lhs_val.compare(.eq, Value.one, lhs_ty)) { | |
| 9579 | if (lhs_val.compare(.eq, Value.one, lhs_ty, target)) { | |
| 9507 | 9580 | return casted_rhs; |
| 9508 | 9581 | } |
| 9509 | 9582 | } |
| ... | ... | @@ -9519,7 +9592,7 @@ fn analyzeArithmetic( |
| 9519 | 9592 | if (rhs_val.compareWithZero(.eq)) { |
| 9520 | 9593 | return sema.addConstant(resolved_type, Value.zero); |
| 9521 | 9594 | } |
| 9522 | if (rhs_val.compare(.eq, Value.one, rhs_ty)) { | |
| 9595 | if (rhs_val.compare(.eq, Value.one, rhs_ty, target)) { | |
| 9523 | 9596 | return casted_lhs; |
| 9524 | 9597 | } |
| 9525 | 9598 | if (maybe_lhs_val) |lhs_val| { |
| ... | ... | @@ -9533,7 +9606,7 @@ fn analyzeArithmetic( |
| 9533 | 9606 | if (is_int) { |
| 9534 | 9607 | return sema.addConstant( |
| 9535 | 9608 | resolved_type, |
| 9536 | try lhs_val.intMul(rhs_val, resolved_type, sema.arena), | |
| 9609 | try lhs_val.intMul(rhs_val, resolved_type, sema.arena, target), | |
| 9537 | 9610 | ); |
| 9538 | 9611 | } else { |
| 9539 | 9612 | return sema.addConstant( |
| ... | ... | @@ -9554,7 +9627,7 @@ fn analyzeArithmetic( |
| 9554 | 9627 | if (lhs_val.compareWithZero(.eq)) { |
| 9555 | 9628 | return sema.addConstant(resolved_type, Value.zero); |
| 9556 | 9629 | } |
| 9557 | if (lhs_val.compare(.eq, Value.one, lhs_ty)) { | |
| 9630 | if (lhs_val.compare(.eq, Value.one, lhs_ty, target)) { | |
| 9558 | 9631 | return casted_rhs; |
| 9559 | 9632 | } |
| 9560 | 9633 | } |
| ... | ... | @@ -9566,7 +9639,7 @@ fn analyzeArithmetic( |
| 9566 | 9639 | if (rhs_val.compareWithZero(.eq)) { |
| 9567 | 9640 | return sema.addConstant(resolved_type, Value.zero); |
| 9568 | 9641 | } |
| 9569 | if (rhs_val.compare(.eq, Value.one, rhs_ty)) { | |
| 9642 | if (rhs_val.compare(.eq, Value.one, rhs_ty, target)) { | |
| 9570 | 9643 | return casted_lhs; |
| 9571 | 9644 | } |
| 9572 | 9645 | if (maybe_lhs_val) |lhs_val| { |
| ... | ... | @@ -9590,7 +9663,7 @@ fn analyzeArithmetic( |
| 9590 | 9663 | if (lhs_val.compareWithZero(.eq)) { |
| 9591 | 9664 | return sema.addConstant(resolved_type, Value.zero); |
| 9592 | 9665 | } |
| 9593 | if (lhs_val.compare(.eq, Value.one, lhs_ty)) { | |
| 9666 | if (lhs_val.compare(.eq, Value.one, lhs_ty, target)) { | |
| 9594 | 9667 | return casted_rhs; |
| 9595 | 9668 | } |
| 9596 | 9669 | } |
| ... | ... | @@ -9602,7 +9675,7 @@ fn analyzeArithmetic( |
| 9602 | 9675 | if (rhs_val.compareWithZero(.eq)) { |
| 9603 | 9676 | return sema.addConstant(resolved_type, Value.zero); |
| 9604 | 9677 | } |
| 9605 | if (rhs_val.compare(.eq, Value.one, rhs_ty)) { | |
| 9678 | if (rhs_val.compare(.eq, Value.one, rhs_ty, target)) { | |
| 9606 | 9679 | return casted_lhs; |
| 9607 | 9680 | } |
| 9608 | 9681 | if (maybe_lhs_val) |lhs_val| { |
| ... | ... | @@ -9611,7 +9684,7 @@ fn analyzeArithmetic( |
| 9611 | 9684 | } |
| 9612 | 9685 | |
| 9613 | 9686 | const val = if (scalar_tag == .ComptimeInt) |
| 9614 | try lhs_val.intMul(rhs_val, resolved_type, sema.arena) | |
| 9687 | try lhs_val.intMul(rhs_val, resolved_type, sema.arena, target) | |
| 9615 | 9688 | else |
| 9616 | 9689 | try lhs_val.intMulSat(rhs_val, resolved_type, sema.arena, target); |
| 9617 | 9690 | |
| ... | ... | @@ -9652,7 +9725,7 @@ fn analyzeArithmetic( |
| 9652 | 9725 | return sema.failWithDivideByZero(block, rhs_src); |
| 9653 | 9726 | } |
| 9654 | 9727 | if (maybe_lhs_val) |lhs_val| { |
| 9655 | const rem_result = try lhs_val.intRem(rhs_val, resolved_type, sema.arena); | |
| 9728 | const rem_result = try lhs_val.intRem(rhs_val, resolved_type, sema.arena, target); | |
| 9656 | 9729 | // If this answer could possibly be different by doing `intMod`, |
| 9657 | 9730 | // we must emit a compile error. Otherwise, it's OK. |
| 9658 | 9731 | if (rhs_val.compareWithZero(.lt) != lhs_val.compareWithZero(.lt) and |
| ... | ... | @@ -9731,7 +9804,7 @@ fn analyzeArithmetic( |
| 9731 | 9804 | if (maybe_lhs_val) |lhs_val| { |
| 9732 | 9805 | return sema.addConstant( |
| 9733 | 9806 | resolved_type, |
| 9734 | try lhs_val.intRem(rhs_val, resolved_type, sema.arena), | |
| 9807 | try lhs_val.intRem(rhs_val, resolved_type, sema.arena, target), | |
| 9735 | 9808 | ); |
| 9736 | 9809 | } |
| 9737 | 9810 | break :rs .{ .src = lhs_src, .air_tag = .rem }; |
| ... | ... | @@ -9788,7 +9861,7 @@ fn analyzeArithmetic( |
| 9788 | 9861 | if (maybe_lhs_val) |lhs_val| { |
| 9789 | 9862 | return sema.addConstant( |
| 9790 | 9863 | resolved_type, |
| 9791 | try lhs_val.intMod(rhs_val, resolved_type, sema.arena), | |
| 9864 | try lhs_val.intMod(rhs_val, resolved_type, sema.arena, target), | |
| 9792 | 9865 | ); |
| 9793 | 9866 | } |
| 9794 | 9867 | break :rs .{ .src = lhs_src, .air_tag = .mod }; |
| ... | ... | @@ -9839,6 +9912,7 @@ fn analyzePtrArithmetic( |
| 9839 | 9912 | // coerce to isize instead of usize. |
| 9840 | 9913 | const offset = try sema.coerce(block, Type.usize, uncasted_offset, offset_src); |
| 9841 | 9914 | // TODO adjust the return type according to alignment and other factors |
| 9915 | const target = sema.mod.getTarget(); | |
| 9842 | 9916 | const runtime_src = rs: { |
| 9843 | 9917 | if (try sema.resolveMaybeUndefVal(block, ptr_src, ptr)) |ptr_val| { |
| 9844 | 9918 | if (try sema.resolveMaybeUndefVal(block, offset_src, offset)) |offset_val| { |
| ... | ... | @@ -9849,11 +9923,10 @@ fn analyzePtrArithmetic( |
| 9849 | 9923 | return sema.addConstUndef(new_ptr_ty); |
| 9850 | 9924 | } |
| 9851 | 9925 | |
| 9852 | const offset_int = try sema.usizeCast(block, offset_src, offset_val.toUnsignedInt()); | |
| 9926 | const offset_int = try sema.usizeCast(block, offset_src, offset_val.toUnsignedInt(target)); | |
| 9853 | 9927 | // TODO I tried to put this check earlier but it the LLVM backend generate invalid instructinons |
| 9854 | 9928 | if (offset_int == 0) return ptr; |
| 9855 | if (ptr_val.getUnsignedInt()) |addr| { | |
| 9856 | const target = sema.mod.getTarget(); | |
| 9929 | if (try ptr_val.getUnsignedIntAdvanced(target, sema.kit(block, ptr_src))) |addr| { | |
| 9857 | 9930 | const ptr_child_ty = ptr_ty.childType(); |
| 9858 | 9931 | const elem_ty = if (ptr_ty.isSinglePointer() and ptr_child_ty.zigTypeTag() == .Array) |
| 9859 | 9932 | ptr_child_ty.childType() |
| ... | ... | @@ -9872,7 +9945,7 @@ fn analyzePtrArithmetic( |
| 9872 | 9945 | if (air_tag == .ptr_sub) { |
| 9873 | 9946 | return sema.fail(block, op_src, "TODO implement Sema comptime pointer subtraction", .{}); |
| 9874 | 9947 | } |
| 9875 | const new_ptr_val = try ptr_val.elemPtr(ptr_ty, sema.arena, offset_int); | |
| 9948 | const new_ptr_val = try ptr_val.elemPtr(ptr_ty, sema.arena, offset_int, target); | |
| 9876 | 9949 | return sema.addConstant(new_ptr_ty, new_ptr_val); |
| 9877 | 9950 | } else break :rs offset_src; |
| 9878 | 9951 | } else break :rs ptr_src; |
| ... | ... | @@ -10035,6 +10108,7 @@ fn zirCmpEq( |
| 10035 | 10108 | const rhs_src: LazySrcLoc = .{ .node_offset_bin_rhs = inst_data.src_node }; |
| 10036 | 10109 | const lhs = sema.resolveInst(extra.lhs); |
| 10037 | 10110 | const rhs = sema.resolveInst(extra.rhs); |
| 10111 | const target = sema.mod.getTarget(); | |
| 10038 | 10112 | |
| 10039 | 10113 | const lhs_ty = sema.typeOf(lhs); |
| 10040 | 10114 | const rhs_ty = sema.typeOf(rhs); |
| ... | ... | @@ -10059,7 +10133,7 @@ fn zirCmpEq( |
| 10059 | 10133 | |
| 10060 | 10134 | if (lhs_ty_tag == .Null or rhs_ty_tag == .Null) { |
| 10061 | 10135 | const non_null_type = if (lhs_ty_tag == .Null) rhs_ty else lhs_ty; |
| 10062 | return sema.fail(block, src, "comparison of '{}' with null", .{non_null_type}); | |
| 10136 | return sema.fail(block, src, "comparison of '{}' with null", .{non_null_type.fmt(target)}); | |
| 10063 | 10137 | } |
| 10064 | 10138 | |
| 10065 | 10139 | if (lhs_ty_tag == .Union and (rhs_ty_tag == .EnumLiteral or rhs_ty_tag == .Enum)) { |
| ... | ... | @@ -10099,7 +10173,7 @@ fn zirCmpEq( |
| 10099 | 10173 | if (lhs_ty_tag == .Type and rhs_ty_tag == .Type) { |
| 10100 | 10174 | const lhs_as_type = try sema.analyzeAsType(block, lhs_src, lhs); |
| 10101 | 10175 | const rhs_as_type = try sema.analyzeAsType(block, rhs_src, rhs); |
| 10102 | if (lhs_as_type.eql(rhs_as_type) == (op == .eq)) { | |
| 10176 | if (lhs_as_type.eql(rhs_as_type, target) == (op == .eq)) { | |
| 10103 | 10177 | return Air.Inst.Ref.bool_true; |
| 10104 | 10178 | } else { |
| 10105 | 10179 | return Air.Inst.Ref.bool_false; |
| ... | ... | @@ -10176,9 +10250,10 @@ fn analyzeCmp( |
| 10176 | 10250 | } |
| 10177 | 10251 | const instructions = &[_]Air.Inst.Ref{ lhs, rhs }; |
| 10178 | 10252 | const resolved_type = try sema.resolvePeerTypes(block, src, instructions, .{ .override = &[_]LazySrcLoc{ lhs_src, rhs_src } }); |
| 10253 | const target = sema.mod.getTarget(); | |
| 10179 | 10254 | if (!resolved_type.isSelfComparable(is_equality_cmp)) { |
| 10180 | 10255 | return sema.fail(block, src, "{s} operator not allowed for type '{}'", .{ |
| 10181 | @tagName(op), resolved_type, | |
| 10256 | @tagName(op), resolved_type.fmt(target), | |
| 10182 | 10257 | }); |
| 10183 | 10258 | } |
| 10184 | 10259 | const casted_lhs = try sema.coerce(block, resolved_type, lhs, lhs_src); |
| ... | ... | @@ -10196,6 +10271,7 @@ fn cmpSelf( |
| 10196 | 10271 | rhs_src: LazySrcLoc, |
| 10197 | 10272 | ) CompileError!Air.Inst.Ref { |
| 10198 | 10273 | const resolved_type = sema.typeOf(casted_lhs); |
| 10274 | const target = sema.mod.getTarget(); | |
| 10199 | 10275 | const runtime_src: LazySrcLoc = src: { |
| 10200 | 10276 | if (try sema.resolveMaybeUndefVal(block, lhs_src, casted_lhs)) |lhs_val| { |
| 10201 | 10277 | if (lhs_val.isUndef()) return sema.addConstUndef(Type.bool); |
| ... | ... | @@ -10204,11 +10280,11 @@ fn cmpSelf( |
| 10204 | 10280 | |
| 10205 | 10281 | if (resolved_type.zigTypeTag() == .Vector) { |
| 10206 | 10282 | const result_ty = try Type.vector(sema.arena, resolved_type.vectorLen(), Type.@"bool"); |
| 10207 | const cmp_val = try lhs_val.compareVector(op, rhs_val, resolved_type, sema.arena); | |
| 10283 | const cmp_val = try lhs_val.compareVector(op, rhs_val, resolved_type, sema.arena, target); | |
| 10208 | 10284 | return sema.addConstant(result_ty, cmp_val); |
| 10209 | 10285 | } |
| 10210 | 10286 | |
| 10211 | if (lhs_val.compare(op, rhs_val, resolved_type)) { | |
| 10287 | if (lhs_val.compare(op, rhs_val, resolved_type, target)) { | |
| 10212 | 10288 | return Air.Inst.Ref.bool_true; |
| 10213 | 10289 | } else { |
| 10214 | 10290 | return Air.Inst.Ref.bool_false; |
| ... | ... | @@ -10276,7 +10352,7 @@ fn zirSizeOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 10276 | 10352 | .Null, |
| 10277 | 10353 | .BoundFn, |
| 10278 | 10354 | .Opaque, |
| 10279 | => return sema.fail(block, src, "no size available for type '{}'", .{operand_ty}), | |
| 10355 | => return sema.fail(block, src, "no size available for type '{}'", .{operand_ty.fmt(target)}), | |
| 10280 | 10356 | |
| 10281 | 10357 | .Type, |
| 10282 | 10358 | .EnumLiteral, |
| ... | ... | @@ -11365,11 +11441,12 @@ fn log2IntType(sema: *Sema, block: *Block, operand: Type, src: LazySrcLoc) Compi |
| 11365 | 11441 | }, |
| 11366 | 11442 | else => {}, |
| 11367 | 11443 | } |
| 11444 | const target = sema.mod.getTarget(); | |
| 11368 | 11445 | return sema.fail( |
| 11369 | 11446 | block, |
| 11370 | 11447 | src, |
| 11371 | 11448 | "bit shifting operation expected integer type, found '{}'", |
| 11372 | .{operand}, | |
| 11449 | .{operand.fmt(target)}, | |
| 11373 | 11450 | ); |
| 11374 | 11451 | } |
| 11375 | 11452 | |
| ... | ... | @@ -11786,6 +11863,8 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 11786 | 11863 | const elem_ty_src: LazySrcLoc = .unneeded; |
| 11787 | 11864 | const inst_data = sema.code.instructions.items(.data)[inst].ptr_type; |
| 11788 | 11865 | const extra = sema.code.extraData(Zir.Inst.PtrType, inst_data.payload_index); |
| 11866 | const unresolved_elem_ty = try sema.resolveType(block, elem_ty_src, extra.data.elem_type); | |
| 11867 | const target = sema.mod.getTarget(); | |
| 11789 | 11868 | |
| 11790 | 11869 | var extra_i = extra.end; |
| 11791 | 11870 | |
| ... | ... | @@ -11795,10 +11874,19 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 11795 | 11874 | break :blk (try sema.resolveInstConst(block, .unneeded, ref)).val; |
| 11796 | 11875 | } else null; |
| 11797 | 11876 | |
| 11798 | const abi_align = if (inst_data.flags.has_align) blk: { | |
| 11877 | const abi_align: u32 = if (inst_data.flags.has_align) blk: { | |
| 11799 | 11878 | const ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_i]); |
| 11800 | 11879 | extra_i += 1; |
| 11801 | const abi_align = try sema.resolveInt(block, .unneeded, ref, Type.u32); | |
| 11880 | const coerced = try sema.coerce(block, Type.u32, sema.resolveInst(ref), src); | |
| 11881 | const val = try sema.resolveConstValue(block, src, coerced); | |
| 11882 | // Check if this happens to be the lazy alignment of our element type, in | |
| 11883 | // which case we can make this 0 without resolving it. | |
| 11884 | if (val.castTag(.lazy_align)) |payload| { | |
| 11885 | if (payload.data.eql(unresolved_elem_ty, target)) { | |
| 11886 | break :blk 0; | |
| 11887 | } | |
| 11888 | } | |
| 11889 | const abi_align = (try val.getUnsignedIntAdvanced(target, sema.kit(block, src))).?; | |
| 11802 | 11890 | break :blk @intCast(u32, abi_align); |
| 11803 | 11891 | } else 0; |
| 11804 | 11892 | |
| ... | ... | @@ -11826,7 +11914,6 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 11826 | 11914 | return sema.fail(block, src, "bit offset starts after end of host integer", .{}); |
| 11827 | 11915 | } |
| 11828 | 11916 | |
| 11829 | const unresolved_elem_ty = try sema.resolveType(block, elem_ty_src, extra.data.elem_type); | |
| 11830 | 11917 | const elem_ty = if (abi_align == 0) |
| 11831 | 11918 | unresolved_elem_ty |
| 11832 | 11919 | else t: { |
| ... | ... | @@ -11834,7 +11921,6 @@ fn zirPtrType(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 11834 | 11921 | try sema.resolveTypeLayout(block, elem_ty_src, elem_ty); |
| 11835 | 11922 | break :t elem_ty; |
| 11836 | 11923 | }; |
| 11837 | const target = sema.mod.getTarget(); | |
| 11838 | 11924 | const ty = try Type.ptr(sema.arena, target, .{ |
| 11839 | 11925 | .pointee_type = elem_ty, |
| 11840 | 11926 | .sentinel = sentinel, |
| ... | ... | @@ -12414,6 +12500,7 @@ fn fieldType( |
| 12414 | 12500 | ty_src: LazySrcLoc, |
| 12415 | 12501 | ) CompileError!Air.Inst.Ref { |
| 12416 | 12502 | const resolved_ty = try sema.resolveTypeFields(block, ty_src, aggregate_ty); |
| 12503 | const target = sema.mod.getTarget(); | |
| 12417 | 12504 | switch (resolved_ty.zigTypeTag()) { |
| 12418 | 12505 | .Struct => { |
| 12419 | 12506 | const struct_obj = resolved_ty.castTag(.@"struct").?.data; |
| ... | ... | @@ -12428,7 +12515,7 @@ fn fieldType( |
| 12428 | 12515 | return sema.addType(field.ty); |
| 12429 | 12516 | }, |
| 12430 | 12517 | else => return sema.fail(block, ty_src, "expected struct or union; found '{}'", .{ |
| 12431 | resolved_ty, | |
| 12518 | resolved_ty.fmt(target), | |
| 12432 | 12519 | }), |
| 12433 | 12520 | } |
| 12434 | 12521 | } |
| ... | ... | @@ -12459,11 +12546,11 @@ fn zirAlignOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 12459 | 12546 | const inst_data = sema.code.instructions.items(.data)[inst].un_node; |
| 12460 | 12547 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 12461 | 12548 | const ty = try sema.resolveType(block, operand_src, inst_data.operand); |
| 12462 | const resolved_ty = try sema.resolveTypeFields(block, operand_src, ty); | |
| 12463 | try sema.resolveTypeLayout(block, operand_src, resolved_ty); | |
| 12464 | 12549 | const target = sema.mod.getTarget(); |
| 12465 | const abi_align = resolved_ty.abiAlignment(target); | |
| 12466 | return sema.addIntUnsigned(Type.comptime_int, abi_align); | |
| 12550 | return sema.addConstant( | |
| 12551 | Type.comptime_int, | |
| 12552 | try ty.lazyAbiAlignment(target, sema.arena), | |
| 12553 | ); | |
| 12467 | 12554 | } |
| 12468 | 12555 | |
| 12469 | 12556 | fn zirBoolToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref { |
| ... | ... | @@ -12509,6 +12596,7 @@ fn zirUnaryMath( |
| 12509 | 12596 | const operand = sema.resolveInst(inst_data.operand); |
| 12510 | 12597 | const operand_src: LazySrcLoc = .{ .node_offset_builtin_call_arg0 = inst_data.src_node }; |
| 12511 | 12598 | const operand_ty = sema.typeOf(operand); |
| 12599 | const target = sema.mod.getTarget(); | |
| 12512 | 12600 | |
| 12513 | 12601 | switch (operand_ty.zigTypeTag()) { |
| 12514 | 12602 | .ComptimeFloat, .Float => {}, |
| ... | ... | @@ -12516,13 +12604,12 @@ fn zirUnaryMath( |
| 12516 | 12604 | const scalar_ty = operand_ty.scalarType(); |
| 12517 | 12605 | switch (scalar_ty.zigTypeTag()) { |
| 12518 | 12606 | .ComptimeFloat, .Float => {}, |
| 12519 | else => return sema.fail(block, operand_src, "expected vector of floats or float type, found '{}'", .{scalar_ty}), | |
| 12607 | else => return sema.fail(block, operand_src, "expected vector of floats or float type, found '{}'", .{scalar_ty.fmt(target)}), | |
| 12520 | 12608 | } |
| 12521 | 12609 | }, |
| 12522 | else => return sema.fail(block, operand_src, "expected vector of floats or float type, found '{}'", .{operand_ty}), | |
| 12610 | else => return sema.fail(block, operand_src, "expected vector of floats or float type, found '{}'", .{operand_ty.fmt(target)}), | |
| 12523 | 12611 | } |
| 12524 | 12612 | |
| 12525 | const target = sema.mod.getTarget(); | |
| 12526 | 12613 | switch (operand_ty.zigTypeTag()) { |
| 12527 | 12614 | .Vector => { |
| 12528 | 12615 | const scalar_ty = operand_ty.scalarType(); |
| ... | ... | @@ -12568,6 +12655,7 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 12568 | 12655 | const src = inst_data.src(); |
| 12569 | 12656 | const operand = sema.resolveInst(inst_data.operand); |
| 12570 | 12657 | const operand_ty = sema.typeOf(operand); |
| 12658 | const target = sema.mod.getTarget(); | |
| 12571 | 12659 | |
| 12572 | 12660 | try sema.resolveTypeLayout(block, operand_src, operand_ty); |
| 12573 | 12661 | const enum_ty = switch (operand_ty.zigTypeTag()) { |
| ... | ... | @@ -12590,13 +12678,13 @@ fn zirTagName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 12590 | 12678 | return sema.failWithOwnedErrorMsg(block, msg); |
| 12591 | 12679 | }, |
| 12592 | 12680 | else => return sema.fail(block, operand_src, "expected enum or union; found {}", .{ |
| 12593 | operand_ty, | |
| 12681 | operand_ty.fmt(target), | |
| 12594 | 12682 | }), |
| 12595 | 12683 | }; |
| 12596 | 12684 | const enum_decl = enum_ty.getOwnerDecl(); |
| 12597 | 12685 | const casted_operand = try sema.coerce(block, enum_ty, operand, operand_src); |
| 12598 | 12686 | if (try sema.resolveDefinedValue(block, operand_src, casted_operand)) |val| { |
| 12599 | const field_index = enum_ty.enumTagFieldIndex(val) orelse { | |
| 12687 | const field_index = enum_ty.enumTagFieldIndex(val, target) orelse { | |
| 12600 | 12688 | const msg = msg: { |
| 12601 | 12689 | const msg = try sema.errMsg(block, src, "no field with value {} in enum '{s}'", .{ |
| 12602 | 12690 | casted_operand, enum_decl.name, |
| ... | ... | @@ -12626,8 +12714,8 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12626 | 12714 | const val = try sema.resolveConstValue(block, operand_src, type_info); |
| 12627 | 12715 | const union_val = val.cast(Value.Payload.Union).?.data; |
| 12628 | 12716 | const tag_ty = type_info_ty.unionTagType().?; |
| 12629 | const tag_index = tag_ty.enumTagFieldIndex(union_val.tag).?; | |
| 12630 | 12717 | const target = sema.mod.getTarget(); |
| 12718 | const tag_index = tag_ty.enumTagFieldIndex(union_val.tag, target).?; | |
| 12631 | 12719 | switch (@intToEnum(std.builtin.TypeId, tag_index)) { |
| 12632 | 12720 | .Type => return Air.Inst.Ref.type_type, |
| 12633 | 12721 | .Void => return Air.Inst.Ref.void_type, |
| ... | ... | @@ -12646,7 +12734,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12646 | 12734 | const bits_val = struct_val[1]; |
| 12647 | 12735 | |
| 12648 | 12736 | const signedness = signedness_val.toEnum(std.builtin.Signedness); |
| 12649 | const bits = @intCast(u16, bits_val.toUnsignedInt()); | |
| 12737 | const bits = @intCast(u16, bits_val.toUnsignedInt(target)); | |
| 12650 | 12738 | const ty = switch (signedness) { |
| 12651 | 12739 | .signed => try Type.Tag.int_signed.create(sema.arena, bits), |
| 12652 | 12740 | .unsigned => try Type.Tag.int_unsigned.create(sema.arena, bits), |
| ... | ... | @@ -12659,7 +12747,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12659 | 12747 | const len_val = struct_val[0]; |
| 12660 | 12748 | const child_val = struct_val[1]; |
| 12661 | 12749 | |
| 12662 | const len = len_val.toUnsignedInt(); | |
| 12750 | const len = len_val.toUnsignedInt(target); | |
| 12663 | 12751 | var buffer: Value.ToTypeBuffer = undefined; |
| 12664 | 12752 | const child_ty = child_val.toType(&buffer); |
| 12665 | 12753 | |
| ... | ... | @@ -12672,7 +12760,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12672 | 12760 | // bits: comptime_int, |
| 12673 | 12761 | const bits_val = struct_val[0]; |
| 12674 | 12762 | |
| 12675 | const bits = @intCast(u16, bits_val.toUnsignedInt()); | |
| 12763 | const bits = @intCast(u16, bits_val.toUnsignedInt(target)); | |
| 12676 | 12764 | const ty = switch (bits) { |
| 12677 | 12765 | 16 => Type.@"f16", |
| 12678 | 12766 | 32 => Type.@"f32", |
| ... | ... | @@ -12717,7 +12805,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12717 | 12805 | .size = ptr_size, |
| 12718 | 12806 | .mutable = !is_const_val.toBool(), |
| 12719 | 12807 | .@"volatile" = is_volatile_val.toBool(), |
| 12720 | .@"align" = @intCast(u16, alignment_val.toUnsignedInt()), // TODO: Validate this value. | |
| 12808 | .@"align" = @intCast(u16, alignment_val.toUnsignedInt(target)), // TODO: Validate this value. | |
| 12721 | 12809 | .@"addrspace" = address_space_val.toEnum(std.builtin.AddressSpace), |
| 12722 | 12810 | .pointee_type = try child_ty.copy(sema.arena), |
| 12723 | 12811 | .@"allowzero" = is_allowzero_val.toBool(), |
| ... | ... | @@ -12735,7 +12823,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12735 | 12823 | // sentinel: ?*const anyopaque, |
| 12736 | 12824 | const sentinel_val = struct_val[2]; |
| 12737 | 12825 | |
| 12738 | const len = len_val.toUnsignedInt(); | |
| 12826 | const len = len_val.toUnsignedInt(target); | |
| 12739 | 12827 | var buffer: Value.ToTypeBuffer = undefined; |
| 12740 | 12828 | const child_ty = try child_val.toType(&buffer).copy(sema.arena); |
| 12741 | 12829 | const sentinel = if (sentinel_val.castTag(.opt_payload)) |p| blk: { |
| ... | ... | @@ -12746,7 +12834,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12746 | 12834 | break :blk (try sema.pointerDeref(block, src, p.data, ptr_ty)).?; |
| 12747 | 12835 | } else null; |
| 12748 | 12836 | |
| 12749 | const ty = try Type.array(sema.arena, len, sentinel, child_ty); | |
| 12837 | const ty = try Type.array(sema.arena, len, sentinel, child_ty, target); | |
| 12750 | 12838 | return sema.addType(ty); |
| 12751 | 12839 | }, |
| 12752 | 12840 | .Optional => { |
| ... | ... | @@ -12796,7 +12884,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12796 | 12884 | const name_val = struct_val[0]; |
| 12797 | 12885 | |
| 12798 | 12886 | names.putAssumeCapacityNoClobber( |
| 12799 | try name_val.toAllocatedBytes(Type.initTag(.const_slice_u8), sema.arena), | |
| 12887 | try name_val.toAllocatedBytes(Type.initTag(.const_slice_u8), sema.arena, target), | |
| 12800 | 12888 | {}, |
| 12801 | 12889 | ); |
| 12802 | 12890 | } |
| ... | ... | @@ -12817,7 +12905,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12817 | 12905 | const is_tuple_val = struct_val[3]; |
| 12818 | 12906 | |
| 12819 | 12907 | // Decls |
| 12820 | if (decls_val.sliceLen() > 0) { | |
| 12908 | if (decls_val.sliceLen(target) > 0) { | |
| 12821 | 12909 | return sema.fail(block, src, "reified structs must have no decls", .{}); |
| 12822 | 12910 | } |
| 12823 | 12911 | |
| ... | ... | @@ -12847,7 +12935,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12847 | 12935 | } |
| 12848 | 12936 | |
| 12849 | 12937 | // Decls |
| 12850 | if (decls_val.sliceLen() > 0) { | |
| 12938 | if (decls_val.sliceLen(target) > 0) { | |
| 12851 | 12939 | return sema.fail(block, src, "reified enums must have no decls", .{}); |
| 12852 | 12940 | } |
| 12853 | 12941 | |
| ... | ... | @@ -12898,11 +12986,12 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12898 | 12986 | enum_obj.tag_ty = try tag_type_val.toType(&buffer).copy(new_decl_arena_allocator); |
| 12899 | 12987 | |
| 12900 | 12988 | // Fields |
| 12901 | const fields_len = try sema.usizeCast(block, src, fields_val.sliceLen()); | |
| 12989 | const fields_len = try sema.usizeCast(block, src, fields_val.sliceLen(target)); | |
| 12902 | 12990 | if (fields_len > 0) { |
| 12903 | 12991 | try enum_obj.fields.ensureTotalCapacity(new_decl_arena_allocator, fields_len); |
| 12904 | 12992 | try enum_obj.values.ensureTotalCapacityContext(new_decl_arena_allocator, fields_len, .{ |
| 12905 | 12993 | .ty = enum_obj.tag_ty, |
| 12994 | .target = target, | |
| 12906 | 12995 | }); |
| 12907 | 12996 | |
| 12908 | 12997 | var i: usize = 0; |
| ... | ... | @@ -12918,6 +13007,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12918 | 13007 | const field_name = try name_val.toAllocatedBytes( |
| 12919 | 13008 | Type.initTag(.const_slice_u8), |
| 12920 | 13009 | new_decl_arena_allocator, |
| 13010 | target, | |
| 12921 | 13011 | ); |
| 12922 | 13012 | |
| 12923 | 13013 | const gop = enum_obj.fields.getOrPutAssumeCapacity(field_name); |
| ... | ... | @@ -12929,6 +13019,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12929 | 13019 | const copied_tag_val = try value_val.copy(new_decl_arena_allocator); |
| 12930 | 13020 | enum_obj.values.putAssumeCapacityNoClobberContext(copied_tag_val, {}, .{ |
| 12931 | 13021 | .ty = enum_obj.tag_ty, |
| 13022 | .target = target, | |
| 12932 | 13023 | }); |
| 12933 | 13024 | } |
| 12934 | 13025 | } |
| ... | ... | @@ -12942,7 +13033,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12942 | 13033 | const decls_val = struct_val[0]; |
| 12943 | 13034 | |
| 12944 | 13035 | // Decls |
| 12945 | if (decls_val.sliceLen() > 0) { | |
| 13036 | if (decls_val.sliceLen(target) > 0) { | |
| 12946 | 13037 | return sema.fail(block, src, "reified opaque must have no decls", .{}); |
| 12947 | 13038 | } |
| 12948 | 13039 | |
| ... | ... | @@ -12993,7 +13084,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 12993 | 13084 | const decls_val = struct_val[3]; |
| 12994 | 13085 | |
| 12995 | 13086 | // Decls |
| 12996 | if (decls_val.sliceLen() > 0) { | |
| 13087 | if (decls_val.sliceLen(target) > 0) { | |
| 12997 | 13088 | return sema.fail(block, src, "reified unions must have no decls", .{}); |
| 12998 | 13089 | } |
| 12999 | 13090 | |
| ... | ... | @@ -13033,7 +13124,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 13033 | 13124 | }; |
| 13034 | 13125 | |
| 13035 | 13126 | // Tag type |
| 13036 | const fields_len = try sema.usizeCast(block, src, fields_val.sliceLen()); | |
| 13127 | const fields_len = try sema.usizeCast(block, src, fields_val.sliceLen(target)); | |
| 13037 | 13128 | union_obj.tag_ty = if (tag_type_val.optionalValue()) |payload_val| blk: { |
| 13038 | 13129 | var buffer: Value.ToTypeBuffer = undefined; |
| 13039 | 13130 | break :blk try payload_val.toType(&buffer).copy(new_decl_arena_allocator); |
| ... | ... | @@ -13058,6 +13149,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 13058 | 13149 | const field_name = try name_val.toAllocatedBytes( |
| 13059 | 13150 | Type.initTag(.const_slice_u8), |
| 13060 | 13151 | new_decl_arena_allocator, |
| 13152 | target, | |
| 13061 | 13153 | ); |
| 13062 | 13154 | |
| 13063 | 13155 | const gop = union_obj.fields.getOrPutAssumeCapacity(field_name); |
| ... | ... | @@ -13069,7 +13161,7 @@ fn zirReify(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.I |
| 13069 | 13161 | var buffer: Value.ToTypeBuffer = undefined; |
| 13070 | 13162 | gop.value_ptr.* = .{ |
| 13071 | 13163 | .ty = try field_type_val.toType(&buffer).copy(new_decl_arena_allocator), |
| 13072 | .abi_align = @intCast(u32, alignment_val.toUnsignedInt()), | |
| 13164 | .abi_align = @intCast(u32, alignment_val.toUnsignedInt(target)), | |
| 13073 | 13165 | }; |
| 13074 | 13166 | } |
| 13075 | 13167 | } |
| ... | ... | @@ -13089,7 +13181,9 @@ fn reifyTuple( |
| 13089 | 13181 | src: LazySrcLoc, |
| 13090 | 13182 | fields_val: Value, |
| 13091 | 13183 | ) CompileError!Air.Inst.Ref { |
| 13092 | const fields_len = try sema.usizeCast(block, src, fields_val.sliceLen()); | |
| 13184 | const target = sema.mod.getTarget(); | |
| 13185 | ||
| 13186 | const fields_len = try sema.usizeCast(block, src, fields_val.sliceLen(target)); | |
| 13093 | 13187 | if (fields_len == 0) return sema.addType(Type.initTag(.empty_struct_literal)); |
| 13094 | 13188 | |
| 13095 | 13189 | const types = try sema.arena.alloc(Type, fields_len); |
| ... | ... | @@ -13114,6 +13208,7 @@ fn reifyTuple( |
| 13114 | 13208 | const field_name = try name_val.toAllocatedBytes( |
| 13115 | 13209 | Type.initTag(.const_slice_u8), |
| 13116 | 13210 | sema.arena, |
| 13211 | target, | |
| 13117 | 13212 | ); |
| 13118 | 13213 | |
| 13119 | 13214 | const field_index = std.fmt.parseUnsigned(u32, field_name, 10) catch |err| { |
| ... | ... | @@ -13197,8 +13292,10 @@ fn reifyStruct( |
| 13197 | 13292 | }, |
| 13198 | 13293 | }; |
| 13199 | 13294 | |
| 13295 | const target = sema.mod.getTarget(); | |
| 13296 | ||
| 13200 | 13297 | // Fields |
| 13201 | const fields_len = try sema.usizeCast(block, src, fields_val.sliceLen()); | |
| 13298 | const fields_len = try sema.usizeCast(block, src, fields_val.sliceLen(target)); | |
| 13202 | 13299 | try struct_obj.fields.ensureTotalCapacity(new_decl_arena_allocator, fields_len); |
| 13203 | 13300 | var i: usize = 0; |
| 13204 | 13301 | while (i < fields_len) : (i += 1) { |
| ... | ... | @@ -13219,6 +13316,7 @@ fn reifyStruct( |
| 13219 | 13316 | const field_name = try name_val.toAllocatedBytes( |
| 13220 | 13317 | Type.initTag(.const_slice_u8), |
| 13221 | 13318 | new_decl_arena_allocator, |
| 13319 | target, | |
| 13222 | 13320 | ); |
| 13223 | 13321 | |
| 13224 | 13322 | const gop = struct_obj.fields.getOrPutAssumeCapacity(field_name); |
| ... | ... | @@ -13238,7 +13336,7 @@ fn reifyStruct( |
| 13238 | 13336 | var buffer: Value.ToTypeBuffer = undefined; |
| 13239 | 13337 | gop.value_ptr.* = .{ |
| 13240 | 13338 | .ty = try field_type_val.toType(&buffer).copy(new_decl_arena_allocator), |
| 13241 | .abi_align = @intCast(u32, alignment_val.toUnsignedInt()), | |
| 13339 | .abi_align = @intCast(u32, alignment_val.toUnsignedInt(target)), | |
| 13242 | 13340 | .default_val = default_val, |
| 13243 | 13341 | .is_comptime = is_comptime_val.toBool(), |
| 13244 | 13342 | .offset = undefined, |
| ... | ... | @@ -13257,7 +13355,8 @@ fn zirTypeName(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 13257 | 13355 | var anon_decl = try block.startAnonDecl(LazySrcLoc.unneeded); |
| 13258 | 13356 | defer anon_decl.deinit(); |
| 13259 | 13357 | |
| 13260 | const bytes = try ty.nameAllocArena(anon_decl.arena()); | |
| 13358 | const target = sema.mod.getTarget(); | |
| 13359 | const bytes = try ty.nameAllocArena(anon_decl.arena(), target); | |
| 13261 | 13360 | |
| 13262 | 13361 | const new_decl = try anon_decl.finish( |
| 13263 | 13362 | try Type.Tag.array_u8_sentinel_0.create(anon_decl.arena(), bytes.len), |
| ... | ... | @@ -13296,7 +13395,10 @@ fn zirFloatToInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 13296 | 13395 | const target = sema.mod.getTarget(); |
| 13297 | 13396 | const result_val = val.floatToInt(sema.arena, operand_ty, dest_ty, target) catch |err| switch (err) { |
| 13298 | 13397 | error.FloatCannotFit => { |
| 13299 | return sema.fail(block, operand_src, "integer value {d} cannot be stored in type '{}'", .{ std.math.floor(val.toFloat(f64)), dest_ty }); | |
| 13398 | return sema.fail(block, operand_src, "integer value {d} cannot be stored in type '{}'", .{ | |
| 13399 | std.math.floor(val.toFloat(f64)), | |
| 13400 | dest_ty.fmt(target), | |
| 13401 | }); | |
| 13300 | 13402 | }, |
| 13301 | 13403 | else => |e| return e, |
| 13302 | 13404 | }; |
| ... | ... | @@ -13344,13 +13446,14 @@ fn zirIntToPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 13344 | 13446 | try sema.checkPtrType(block, type_src, type_res); |
| 13345 | 13447 | try sema.resolveTypeLayout(block, src, type_res.elemType2()); |
| 13346 | 13448 | const ptr_align = type_res.ptrAlignment(sema.mod.getTarget()); |
| 13449 | const target = sema.mod.getTarget(); | |
| 13347 | 13450 | |
| 13348 | 13451 | if (try sema.resolveDefinedValue(block, operand_src, operand_coerced)) |val| { |
| 13349 | const addr = val.toUnsignedInt(); | |
| 13452 | const addr = val.toUnsignedInt(target); | |
| 13350 | 13453 | if (!type_res.isAllowzeroPtr() and addr == 0) |
| 13351 | return sema.fail(block, operand_src, "pointer type '{}' does not allow address zero", .{type_res}); | |
| 13454 | return sema.fail(block, operand_src, "pointer type '{}' does not allow address zero", .{type_res.fmt(target)}); | |
| 13352 | 13455 | if (addr != 0 and addr % ptr_align != 0) |
| 13353 | return sema.fail(block, operand_src, "pointer type '{}' requires aligned address", .{type_res}); | |
| 13456 | return sema.fail(block, operand_src, "pointer type '{}' requires aligned address", .{type_res.fmt(target)}); | |
| 13354 | 13457 | |
| 13355 | 13458 | const val_payload = try sema.arena.create(Value.Payload.U64); |
| 13356 | 13459 | val_payload.* = .{ |
| ... | ... | @@ -13394,6 +13497,7 @@ fn zirErrSetCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 13394 | 13497 | const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs); |
| 13395 | 13498 | const operand = sema.resolveInst(extra.rhs); |
| 13396 | 13499 | const operand_ty = sema.typeOf(operand); |
| 13500 | const target = sema.mod.getTarget(); | |
| 13397 | 13501 | try sema.checkErrorSetType(block, dest_ty_src, dest_ty); |
| 13398 | 13502 | try sema.checkErrorSetType(block, operand_src, operand_ty); |
| 13399 | 13503 | |
| ... | ... | @@ -13407,7 +13511,7 @@ fn zirErrSetCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! |
| 13407 | 13511 | block, |
| 13408 | 13512 | src, |
| 13409 | 13513 | "error.{s} not a member of error set '{}'", |
| 13410 | .{ error_name, dest_ty }, | |
| 13514 | .{ error_name, dest_ty.fmt(target) }, | |
| 13411 | 13515 | ); |
| 13412 | 13516 | } |
| 13413 | 13517 | } |
| ... | ... | @@ -13502,7 +13606,7 @@ fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 13502 | 13606 | |
| 13503 | 13607 | if (operand_info.signedness != dest_info.signedness) { |
| 13504 | 13608 | return sema.fail(block, operand_src, "expected {s} integer type, found '{}'", .{ |
| 13505 | @tagName(dest_info.signedness), operand_ty, | |
| 13609 | @tagName(dest_info.signedness), operand_ty.fmt(target), | |
| 13506 | 13610 | }); |
| 13507 | 13611 | } |
| 13508 | 13612 | if (operand_info.bits < dest_info.bits) { |
| ... | ... | @@ -13511,7 +13615,7 @@ fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 13511 | 13615 | block, |
| 13512 | 13616 | src, |
| 13513 | 13617 | "destination type '{}' has more bits than source type '{}'", |
| 13514 | .{ dest_ty, operand_ty }, | |
| 13618 | .{ dest_ty.fmt(target), operand_ty.fmt(target) }, | |
| 13515 | 13619 | ); |
| 13516 | 13620 | errdefer msg.destroy(sema.gpa); |
| 13517 | 13621 | try sema.errNote(block, dest_ty_src, msg, "destination type has {d} bits", .{ |
| ... | ... | @@ -13531,14 +13635,14 @@ fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 13531 | 13635 | if (!is_vector) { |
| 13532 | 13636 | return sema.addConstant( |
| 13533 | 13637 | dest_ty, |
| 13534 | try val.intTrunc(operand_ty, sema.arena, dest_info.signedness, dest_info.bits), | |
| 13638 | try val.intTrunc(operand_ty, sema.arena, dest_info.signedness, dest_info.bits, target), | |
| 13535 | 13639 | ); |
| 13536 | 13640 | } |
| 13537 | 13641 | var elem_buf: Value.ElemValueBuffer = undefined; |
| 13538 | 13642 | const elems = try sema.arena.alloc(Value, operand_ty.vectorLen()); |
| 13539 | 13643 | for (elems) |*elem, i| { |
| 13540 | 13644 | const elem_val = val.elemValueBuffer(i, &elem_buf); |
| 13541 | elem.* = try elem_val.intTrunc(operand_scalar_ty, sema.arena, dest_info.signedness, dest_info.bits); | |
| 13645 | elem.* = try elem_val.intTrunc(operand_scalar_ty, sema.arena, dest_info.signedness, dest_info.bits, target); | |
| 13542 | 13646 | } |
| 13543 | 13647 | return sema.addConstant( |
| 13544 | 13648 | dest_ty, |
| ... | ... | @@ -13653,7 +13757,7 @@ fn zirByteSwap(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai |
| 13653 | 13757 | block, |
| 13654 | 13758 | ty_src, |
| 13655 | 13759 | "@byteSwap requires the number of bits to be evenly divisible by 8, but {} has {} bits", |
| 13656 | .{ scalar_ty, bits }, | |
| 13760 | .{ scalar_ty.fmt(target), bits }, | |
| 13657 | 13761 | ); |
| 13658 | 13762 | } |
| 13659 | 13763 | |
| ... | ... | @@ -13765,6 +13869,7 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6 |
| 13765 | 13869 | |
| 13766 | 13870 | const ty = try sema.resolveType(block, lhs_src, extra.lhs); |
| 13767 | 13871 | const field_name = try sema.resolveConstString(block, rhs_src, extra.rhs); |
| 13872 | const target = sema.mod.getTarget(); | |
| 13768 | 13873 | |
| 13769 | 13874 | try sema.resolveTypeLayout(block, lhs_src, ty); |
| 13770 | 13875 | if (ty.tag() != .@"struct") { |
| ... | ... | @@ -13772,7 +13877,7 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6 |
| 13772 | 13877 | block, |
| 13773 | 13878 | lhs_src, |
| 13774 | 13879 | "expected struct type, found '{}'", |
| 13775 | .{ty}, | |
| 13880 | .{ty.fmt(target)}, | |
| 13776 | 13881 | ); |
| 13777 | 13882 | } |
| 13778 | 13883 | |
| ... | ... | @@ -13782,11 +13887,10 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6 |
| 13782 | 13887 | block, |
| 13783 | 13888 | rhs_src, |
| 13784 | 13889 | "struct '{}' has no field '{s}'", |
| 13785 | .{ ty, field_name }, | |
| 13890 | .{ ty.fmt(target), field_name }, | |
| 13786 | 13891 | ); |
| 13787 | 13892 | }; |
| 13788 | 13893 | |
| 13789 | const target = sema.mod.getTarget(); | |
| 13790 | 13894 | switch (ty.containerLayout()) { |
| 13791 | 13895 | .Packed => { |
| 13792 | 13896 | var bit_sum: u64 = 0; |
| ... | ... | @@ -13809,18 +13913,20 @@ fn bitOffsetOf(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!u6 |
| 13809 | 13913 | } |
| 13810 | 13914 | |
| 13811 | 13915 | fn checkNamespaceType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!void { |
| 13916 | const target = sema.mod.getTarget(); | |
| 13812 | 13917 | switch (ty.zigTypeTag()) { |
| 13813 | 13918 | .Struct, .Enum, .Union, .Opaque => return, |
| 13814 | else => return sema.fail(block, src, "expected struct, enum, union, or opaque; found '{}'", .{ty}), | |
| 13919 | else => return sema.fail(block, src, "expected struct, enum, union, or opaque; found '{}'", .{ty.fmt(target)}), | |
| 13815 | 13920 | } |
| 13816 | 13921 | } |
| 13817 | 13922 | |
| 13818 | 13923 | /// Returns `true` if the type was a comptime_int. |
| 13819 | 13924 | fn checkIntType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!bool { |
| 13925 | const target = sema.mod.getTarget(); | |
| 13820 | 13926 | switch (try ty.zigTypeTagOrPoison()) { |
| 13821 | 13927 | .ComptimeInt => return true, |
| 13822 | 13928 | .Int => return false, |
| 13823 | else => return sema.fail(block, src, "expected integer type, found '{}'", .{ty}), | |
| 13929 | else => return sema.fail(block, src, "expected integer type, found '{}'", .{ty.fmt(target)}), | |
| 13824 | 13930 | } |
| 13825 | 13931 | } |
| 13826 | 13932 | |
| ... | ... | @@ -13830,6 +13936,7 @@ fn checkPtrOperand( |
| 13830 | 13936 | ty_src: LazySrcLoc, |
| 13831 | 13937 | ty: Type, |
| 13832 | 13938 | ) CompileError!void { |
| 13939 | const target = sema.mod.getTarget(); | |
| 13833 | 13940 | switch (ty.zigTypeTag()) { |
| 13834 | 13941 | .Pointer => return, |
| 13835 | 13942 | .Fn => { |
| ... | ... | @@ -13838,7 +13945,7 @@ fn checkPtrOperand( |
| 13838 | 13945 | block, |
| 13839 | 13946 | ty_src, |
| 13840 | 13947 | "expected pointer, found {}", |
| 13841 | .{ty}, | |
| 13948 | .{ty.fmt(target)}, | |
| 13842 | 13949 | ); |
| 13843 | 13950 | errdefer msg.destroy(sema.gpa); |
| 13844 | 13951 | |
| ... | ... | @@ -13851,7 +13958,7 @@ fn checkPtrOperand( |
| 13851 | 13958 | .Optional => if (ty.isPtrLikeOptional()) return, |
| 13852 | 13959 | else => {}, |
| 13853 | 13960 | } |
| 13854 | return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty}); | |
| 13961 | return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty.fmt(target)}); | |
| 13855 | 13962 | } |
| 13856 | 13963 | |
| 13857 | 13964 | fn checkPtrType( |
| ... | ... | @@ -13860,6 +13967,7 @@ fn checkPtrType( |
| 13860 | 13967 | ty_src: LazySrcLoc, |
| 13861 | 13968 | ty: Type, |
| 13862 | 13969 | ) CompileError!void { |
| 13970 | const target = sema.mod.getTarget(); | |
| 13863 | 13971 | switch (ty.zigTypeTag()) { |
| 13864 | 13972 | .Pointer => return, |
| 13865 | 13973 | .Fn => { |
| ... | ... | @@ -13868,7 +13976,7 @@ fn checkPtrType( |
| 13868 | 13976 | block, |
| 13869 | 13977 | ty_src, |
| 13870 | 13978 | "expected pointer type, found '{}'", |
| 13871 | .{ty}, | |
| 13979 | .{ty.fmt(target)}, | |
| 13872 | 13980 | ); |
| 13873 | 13981 | errdefer msg.destroy(sema.gpa); |
| 13874 | 13982 | |
| ... | ... | @@ -13881,7 +13989,7 @@ fn checkPtrType( |
| 13881 | 13989 | .Optional => if (ty.isPtrLikeOptional()) return, |
| 13882 | 13990 | else => {}, |
| 13883 | 13991 | } |
| 13884 | return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty}); | |
| 13992 | return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{ty.fmt(target)}); | |
| 13885 | 13993 | } |
| 13886 | 13994 | |
| 13887 | 13995 | fn checkVectorElemType( |
| ... | ... | @@ -13894,7 +14002,8 @@ fn checkVectorElemType( |
| 13894 | 14002 | .Int, .Float, .Bool => return, |
| 13895 | 14003 | else => if (ty.isPtrAtRuntime()) return, |
| 13896 | 14004 | } |
| 13897 | return sema.fail(block, ty_src, "expected integer, float, bool, or pointer for the vector element type; found '{}'", .{ty}); | |
| 14005 | const target = sema.mod.getTarget(); | |
| 14006 | return sema.fail(block, ty_src, "expected integer, float, bool, or pointer for the vector element type; found '{}'", .{ty.fmt(target)}); | |
| 13898 | 14007 | } |
| 13899 | 14008 | |
| 13900 | 14009 | fn checkFloatType( |
| ... | ... | @@ -13903,9 +14012,10 @@ fn checkFloatType( |
| 13903 | 14012 | ty_src: LazySrcLoc, |
| 13904 | 14013 | ty: Type, |
| 13905 | 14014 | ) CompileError!void { |
| 14015 | const target = sema.mod.getTarget(); | |
| 13906 | 14016 | switch (ty.zigTypeTag()) { |
| 13907 | 14017 | .ComptimeInt, .ComptimeFloat, .Float => {}, |
| 13908 | else => return sema.fail(block, ty_src, "expected float type, found '{}'", .{ty}), | |
| 14018 | else => return sema.fail(block, ty_src, "expected float type, found '{}'", .{ty.fmt(target)}), | |
| 13909 | 14019 | } |
| 13910 | 14020 | } |
| 13911 | 14021 | |
| ... | ... | @@ -13915,13 +14025,14 @@ fn checkNumericType( |
| 13915 | 14025 | ty_src: LazySrcLoc, |
| 13916 | 14026 | ty: Type, |
| 13917 | 14027 | ) CompileError!void { |
| 14028 | const target = sema.mod.getTarget(); | |
| 13918 | 14029 | switch (ty.zigTypeTag()) { |
| 13919 | 14030 | .ComptimeFloat, .Float, .ComptimeInt, .Int => {}, |
| 13920 | 14031 | .Vector => switch (ty.childType().zigTypeTag()) { |
| 13921 | 14032 | .ComptimeFloat, .Float, .ComptimeInt, .Int => {}, |
| 13922 | 14033 | else => |t| return sema.fail(block, ty_src, "expected number, found '{}'", .{t}), |
| 13923 | 14034 | }, |
| 13924 | else => return sema.fail(block, ty_src, "expected number, found '{}'", .{ty}), | |
| 14035 | else => return sema.fail(block, ty_src, "expected number, found '{}'", .{ty.fmt(target)}), | |
| 13925 | 14036 | } |
| 13926 | 14037 | } |
| 13927 | 14038 | |
| ... | ... | @@ -13957,7 +14068,7 @@ fn checkAtomicOperandType( |
| 13957 | 14068 | block, |
| 13958 | 14069 | ty_src, |
| 13959 | 14070 | "expected bool, integer, float, enum, or pointer type; found {}", |
| 13960 | .{ty}, | |
| 14071 | .{ty.fmt(target)}, | |
| 13961 | 14072 | ); |
| 13962 | 14073 | }, |
| 13963 | 14074 | }; |
| ... | ... | @@ -14021,6 +14132,7 @@ fn checkIntOrVector( |
| 14021 | 14132 | operand_src: LazySrcLoc, |
| 14022 | 14133 | ) CompileError!Type { |
| 14023 | 14134 | const operand_ty = sema.typeOf(operand); |
| 14135 | const target = sema.mod.getTarget(); | |
| 14024 | 14136 | switch (try operand_ty.zigTypeTagOrPoison()) { |
| 14025 | 14137 | .Int => return operand_ty, |
| 14026 | 14138 | .Vector => { |
| ... | ... | @@ -14028,12 +14140,12 @@ fn checkIntOrVector( |
| 14028 | 14140 | switch (try elem_ty.zigTypeTagOrPoison()) { |
| 14029 | 14141 | .Int => return elem_ty, |
| 14030 | 14142 | else => return sema.fail(block, operand_src, "expected vector of integers; found vector of '{}'", .{ |
| 14031 | elem_ty, | |
| 14143 | elem_ty.fmt(target), | |
| 14032 | 14144 | }), |
| 14033 | 14145 | } |
| 14034 | 14146 | }, |
| 14035 | 14147 | else => return sema.fail(block, operand_src, "expected integer or vector, found '{}'", .{ |
| 14036 | operand_ty, | |
| 14148 | operand_ty.fmt(target), | |
| 14037 | 14149 | }), |
| 14038 | 14150 | } |
| 14039 | 14151 | } |
| ... | ... | @@ -14045,6 +14157,7 @@ fn checkIntOrVectorAllowComptime( |
| 14045 | 14157 | operand_src: LazySrcLoc, |
| 14046 | 14158 | ) CompileError!Type { |
| 14047 | 14159 | const operand_ty = sema.typeOf(operand); |
| 14160 | const target = sema.mod.getTarget(); | |
| 14048 | 14161 | switch (try operand_ty.zigTypeTagOrPoison()) { |
| 14049 | 14162 | .Int, .ComptimeInt => return operand_ty, |
| 14050 | 14163 | .Vector => { |
| ... | ... | @@ -14052,20 +14165,21 @@ fn checkIntOrVectorAllowComptime( |
| 14052 | 14165 | switch (try elem_ty.zigTypeTagOrPoison()) { |
| 14053 | 14166 | .Int, .ComptimeInt => return elem_ty, |
| 14054 | 14167 | else => return sema.fail(block, operand_src, "expected vector of integers; found vector of '{}'", .{ |
| 14055 | elem_ty, | |
| 14168 | elem_ty.fmt(target), | |
| 14056 | 14169 | }), |
| 14057 | 14170 | } |
| 14058 | 14171 | }, |
| 14059 | 14172 | else => return sema.fail(block, operand_src, "expected integer or vector, found '{}'", .{ |
| 14060 | operand_ty, | |
| 14173 | operand_ty.fmt(target), | |
| 14061 | 14174 | }), |
| 14062 | 14175 | } |
| 14063 | 14176 | } |
| 14064 | 14177 | |
| 14065 | 14178 | fn checkErrorSetType(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!void { |
| 14179 | const target = sema.mod.getTarget(); | |
| 14066 | 14180 | switch (ty.zigTypeTag()) { |
| 14067 | 14181 | .ErrorSet => return, |
| 14068 | else => return sema.fail(block, src, "expected error set type, found '{}'", .{ty}), | |
| 14182 | else => return sema.fail(block, src, "expected error set type, found '{}'", .{ty.fmt(target)}), | |
| 14069 | 14183 | } |
| 14070 | 14184 | } |
| 14071 | 14185 | |
| ... | ... | @@ -14138,9 +14252,10 @@ fn checkVectorizableBinaryOperands( |
| 14138 | 14252 | return sema.failWithOwnedErrorMsg(block, msg); |
| 14139 | 14253 | } |
| 14140 | 14254 | } else if (lhs_zig_ty_tag == .Vector or rhs_zig_ty_tag == .Vector) { |
| 14255 | const target = sema.mod.getTarget(); | |
| 14141 | 14256 | const msg = msg: { |
| 14142 | 14257 | const msg = try sema.errMsg(block, src, "mixed scalar and vector operands: {} and {}", .{ |
| 14143 | lhs_ty, rhs_ty, | |
| 14258 | lhs_ty.fmt(target), rhs_ty.fmt(target), | |
| 14144 | 14259 | }); |
| 14145 | 14260 | errdefer msg.destroy(sema.gpa); |
| 14146 | 14261 | if (lhs_zig_ty_tag == .Vector) { |
| ... | ... | @@ -14179,8 +14294,9 @@ fn resolveExportOptions( |
| 14179 | 14294 | return sema.fail(block, src, "TODO: implement exporting with linksection", .{}); |
| 14180 | 14295 | } |
| 14181 | 14296 | const name_ty = Type.initTag(.const_slice_u8); |
| 14297 | const target = sema.mod.getTarget(); | |
| 14182 | 14298 | return std.builtin.ExportOptions{ |
| 14183 | .name = try name_val.toAllocatedBytes(name_ty, sema.arena), | |
| 14299 | .name = try name_val.toAllocatedBytes(name_ty, sema.arena, target), | |
| 14184 | 14300 | .linkage = linkage_val.toEnum(std.builtin.GlobalLinkage), |
| 14185 | 14301 | .section = null, // TODO |
| 14186 | 14302 | }; |
| ... | ... | @@ -14239,12 +14355,13 @@ fn zirCmpxchg( |
| 14239 | 14355 | const ptr_ty = sema.typeOf(ptr); |
| 14240 | 14356 | const elem_ty = ptr_ty.elemType(); |
| 14241 | 14357 | try sema.checkAtomicOperandType(block, elem_ty_src, elem_ty); |
| 14358 | const target = sema.mod.getTarget(); | |
| 14242 | 14359 | if (elem_ty.zigTypeTag() == .Float) { |
| 14243 | 14360 | return sema.fail( |
| 14244 | 14361 | block, |
| 14245 | 14362 | elem_ty_src, |
| 14246 | 14363 | "expected bool, integer, enum, or pointer type; found '{}'", |
| 14247 | .{elem_ty}, | |
| 14364 | .{elem_ty.fmt(target)}, | |
| 14248 | 14365 | ); |
| 14249 | 14366 | } |
| 14250 | 14367 | const expected_value = try sema.coerce(block, elem_ty, sema.resolveInst(extra.expected_value), expected_src); |
| ... | ... | @@ -14281,7 +14398,7 @@ fn zirCmpxchg( |
| 14281 | 14398 | return sema.addConstUndef(result_ty); |
| 14282 | 14399 | } |
| 14283 | 14400 | const stored_val = (try sema.pointerDeref(block, ptr_src, ptr_val, ptr_ty)) orelse break :rs ptr_src; |
| 14284 | const result_val = if (stored_val.eql(expected_val, elem_ty)) blk: { | |
| 14401 | const result_val = if (stored_val.eql(expected_val, elem_ty, target)) blk: { | |
| 14285 | 14402 | try sema.storePtr(block, src, ptr, new_value); |
| 14286 | 14403 | break :blk Value.@"null"; |
| 14287 | 14404 | } else try Value.Tag.opt_payload.create(sema.arena, stored_val); |
| ... | ... | @@ -14343,9 +14460,10 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 14343 | 14460 | const operation = try sema.resolveBuiltinEnum(block, op_src, extra.lhs, "ReduceOp"); |
| 14344 | 14461 | const operand = sema.resolveInst(extra.rhs); |
| 14345 | 14462 | const operand_ty = sema.typeOf(operand); |
| 14463 | const target = sema.mod.getTarget(); | |
| 14346 | 14464 | |
| 14347 | 14465 | if (operand_ty.zigTypeTag() != .Vector) { |
| 14348 | return sema.fail(block, operand_src, "expected vector, found {}", .{operand_ty}); | |
| 14466 | return sema.fail(block, operand_src, "expected vector, found {}", .{operand_ty.fmt(target)}); | |
| 14349 | 14467 | } |
| 14350 | 14468 | |
| 14351 | 14469 | const scalar_ty = operand_ty.childType(); |
| ... | ... | @@ -14355,13 +14473,13 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 14355 | 14473 | .And, .Or, .Xor => switch (scalar_ty.zigTypeTag()) { |
| 14356 | 14474 | .Int, .Bool => {}, |
| 14357 | 14475 | else => return sema.fail(block, operand_src, "@reduce operation '{s}' requires integer or boolean operand; found {}", .{ |
| 14358 | @tagName(operation), operand_ty, | |
| 14476 | @tagName(operation), operand_ty.fmt(target), | |
| 14359 | 14477 | }), |
| 14360 | 14478 | }, |
| 14361 | 14479 | .Min, .Max, .Add, .Mul => switch (scalar_ty.zigTypeTag()) { |
| 14362 | 14480 | .Int, .Float => {}, |
| 14363 | 14481 | else => return sema.fail(block, operand_src, "@reduce operation '{s}' requires integer or float operand; found {}", .{ |
| 14364 | @tagName(operation), operand_ty, | |
| 14482 | @tagName(operation), operand_ty.fmt(target), | |
| 14365 | 14483 | }), |
| 14366 | 14484 | }, |
| 14367 | 14485 | } |
| ... | ... | @@ -14376,18 +14494,17 @@ fn zirReduce(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 14376 | 14494 | if (try sema.resolveMaybeUndefVal(block, operand_src, operand)) |operand_val| { |
| 14377 | 14495 | if (operand_val.isUndef()) return sema.addConstUndef(scalar_ty); |
| 14378 | 14496 | |
| 14379 | const target = sema.mod.getTarget(); | |
| 14380 | 14497 | var accum: Value = try operand_val.elemValue(sema.arena, 0); |
| 14381 | 14498 | var elem_buf: Value.ElemValueBuffer = undefined; |
| 14382 | 14499 | var i: u32 = 1; |
| 14383 | 14500 | while (i < vec_len) : (i += 1) { |
| 14384 | 14501 | const elem_val = operand_val.elemValueBuffer(i, &elem_buf); |
| 14385 | 14502 | switch (operation) { |
| 14386 | .And => accum = try accum.bitwiseAnd(elem_val, scalar_ty, sema.arena), | |
| 14387 | .Or => accum = try accum.bitwiseOr(elem_val, scalar_ty, sema.arena), | |
| 14388 | .Xor => accum = try accum.bitwiseXor(elem_val, scalar_ty, sema.arena), | |
| 14389 | .Min => accum = accum.numberMin(elem_val), | |
| 14390 | .Max => accum = accum.numberMax(elem_val), | |
| 14503 | .And => accum = try accum.bitwiseAnd(elem_val, scalar_ty, sema.arena, target), | |
| 14504 | .Or => accum = try accum.bitwiseOr(elem_val, scalar_ty, sema.arena, target), | |
| 14505 | .Xor => accum = try accum.bitwiseXor(elem_val, scalar_ty, sema.arena, target), | |
| 14506 | .Min => accum = accum.numberMin(elem_val, target), | |
| 14507 | .Max => accum = accum.numberMax(elem_val, target), | |
| 14391 | 14508 | .Add => accum = try accum.numberAddWrap(elem_val, scalar_ty, sema.arena, target), |
| 14392 | 14509 | .Mul => accum = try accum.numberMulWrap(elem_val, scalar_ty, sema.arena, target), |
| 14393 | 14510 | } |
| ... | ... | @@ -14417,10 +14534,11 @@ fn zirShuffle(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air |
| 14417 | 14534 | var b = sema.resolveInst(extra.b); |
| 14418 | 14535 | var mask = sema.resolveInst(extra.mask); |
| 14419 | 14536 | var mask_ty = sema.typeOf(mask); |
| 14537 | const target = sema.mod.getTarget(); | |
| 14420 | 14538 | |
| 14421 | 14539 | const mask_len = switch (sema.typeOf(mask).zigTypeTag()) { |
| 14422 | 14540 | .Array, .Vector => sema.typeOf(mask).arrayLen(), |
| 14423 | else => return sema.fail(block, mask_src, "expected vector or array, found {}", .{sema.typeOf(mask)}), | |
| 14541 | else => return sema.fail(block, mask_src, "expected vector or array, found {}", .{sema.typeOf(mask).fmt(target)}), | |
| 14424 | 14542 | }; |
| 14425 | 14543 | mask_ty = try Type.Tag.vector.create(sema.arena, .{ |
| 14426 | 14544 | .len = mask_len, |
| ... | ... | @@ -14452,20 +14570,21 @@ fn analyzeShuffle( |
| 14452 | 14570 | .elem_type = elem_ty, |
| 14453 | 14571 | }); |
| 14454 | 14572 | |
| 14573 | const target = sema.mod.getTarget(); | |
| 14455 | 14574 | var maybe_a_len = switch (sema.typeOf(a).zigTypeTag()) { |
| 14456 | 14575 | .Array, .Vector => sema.typeOf(a).arrayLen(), |
| 14457 | 14576 | .Undefined => null, |
| 14458 | 14577 | else => return sema.fail(block, a_src, "expected vector or array with element type {}, found {}", .{ |
| 14459 | elem_ty, | |
| 14460 | sema.typeOf(a), | |
| 14578 | elem_ty.fmt(target), | |
| 14579 | sema.typeOf(a).fmt(target), | |
| 14461 | 14580 | }), |
| 14462 | 14581 | }; |
| 14463 | 14582 | var maybe_b_len = switch (sema.typeOf(b).zigTypeTag()) { |
| 14464 | 14583 | .Array, .Vector => sema.typeOf(b).arrayLen(), |
| 14465 | 14584 | .Undefined => null, |
| 14466 | 14585 | else => return sema.fail(block, b_src, "expected vector or array with element type {}, found {}", .{ |
| 14467 | elem_ty, | |
| 14468 | sema.typeOf(b), | |
| 14586 | elem_ty.fmt(target), | |
| 14587 | sema.typeOf(b).fmt(target), | |
| 14469 | 14588 | }), |
| 14470 | 14589 | }; |
| 14471 | 14590 | if (maybe_a_len == null and maybe_b_len == null) { |
| ... | ... | @@ -14513,7 +14632,7 @@ fn analyzeShuffle( |
| 14513 | 14632 | |
| 14514 | 14633 | try sema.errNote(block, operand_info[chosen][1], msg, "selected index {d} out of bounds of {}", .{ |
| 14515 | 14634 | unsigned, |
| 14516 | operand_info[chosen][2], | |
| 14635 | operand_info[chosen][2].fmt(target), | |
| 14517 | 14636 | }); |
| 14518 | 14637 | |
| 14519 | 14638 | if (chosen == 1) { |
| ... | ... | @@ -14704,12 +14823,12 @@ fn zirAtomicRmw(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A |
| 14704 | 14823 | .Xchg => operand_val, |
| 14705 | 14824 | .Add => try stored_val.numberAddWrap(operand_val, operand_ty, sema.arena, target), |
| 14706 | 14825 | .Sub => try stored_val.numberSubWrap(operand_val, operand_ty, sema.arena, target), |
| 14707 | .And => try stored_val.bitwiseAnd (operand_val, operand_ty, sema.arena), | |
| 14826 | .And => try stored_val.bitwiseAnd (operand_val, operand_ty, sema.arena, target), | |
| 14708 | 14827 | .Nand => try stored_val.bitwiseNand (operand_val, operand_ty, sema.arena, target), |
| 14709 | .Or => try stored_val.bitwiseOr (operand_val, operand_ty, sema.arena), | |
| 14710 | .Xor => try stored_val.bitwiseXor (operand_val, operand_ty, sema.arena), | |
| 14711 | .Max => stored_val.numberMax (operand_val), | |
| 14712 | .Min => stored_val.numberMin (operand_val), | |
| 14828 | .Or => try stored_val.bitwiseOr (operand_val, operand_ty, sema.arena, target), | |
| 14829 | .Xor => try stored_val.bitwiseXor (operand_val, operand_ty, sema.arena, target), | |
| 14830 | .Max => stored_val.numberMax (operand_val, target), | |
| 14831 | .Min => stored_val.numberMin (operand_val, target), | |
| 14713 | 14832 | // zig fmt: on |
| 14714 | 14833 | }; |
| 14715 | 14834 | try sema.storePtrVal(block, src, ptr_val, new_val, operand_ty); |
| ... | ... | @@ -14788,7 +14907,7 @@ fn zirMulAdd(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 14788 | 14907 | |
| 14789 | 14908 | switch (ty.zigTypeTag()) { |
| 14790 | 14909 | .ComptimeFloat, .Float, .Vector => {}, |
| 14791 | else => return sema.fail(block, src, "expected vector of floats or float type, found '{}'", .{ty}), | |
| 14910 | else => return sema.fail(block, src, "expected vector of floats or float type, found '{}'", .{ty.fmt(target)}), | |
| 14792 | 14911 | } |
| 14793 | 14912 | |
| 14794 | 14913 | const runtime_src = if (maybe_mulend1) |mulend1_val| rs: { |
| ... | ... | @@ -14814,7 +14933,7 @@ fn zirMulAdd(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air. |
| 14814 | 14933 | const scalar_ty = ty.scalarType(); |
| 14815 | 14934 | switch (scalar_ty.zigTypeTag()) { |
| 14816 | 14935 | .ComptimeFloat, .Float => {}, |
| 14817 | else => return sema.fail(block, src, "expected vector of floats, found vector of '{}'", .{scalar_ty}), | |
| 14936 | else => return sema.fail(block, src, "expected vector of floats, found vector of '{}'", .{scalar_ty.fmt(target)}), | |
| 14818 | 14937 | } |
| 14819 | 14938 | |
| 14820 | 14939 | const vec_len = ty.vectorLen(); |
| ... | ... | @@ -14906,9 +15025,10 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError |
| 14906 | 15025 | break :modifier modifier_val.toEnum(std.builtin.CallOptions.Modifier); |
| 14907 | 15026 | }; |
| 14908 | 15027 | |
| 15028 | const target = sema.mod.getTarget(); | |
| 14909 | 15029 | const args_ty = sema.typeOf(args); |
| 14910 | 15030 | if (!args_ty.isTuple() and args_ty.tag() != .empty_struct_literal) { |
| 14911 | return sema.fail(block, args_src, "expected a tuple, found {}", .{args_ty}); | |
| 15031 | return sema.fail(block, args_src, "expected a tuple, found {}", .{args_ty.fmt(target)}); | |
| 14912 | 15032 | } |
| 14913 | 15033 | |
| 14914 | 15034 | var resolved_args: []Air.Inst.Ref = undefined; |
| ... | ... | @@ -14945,9 +15065,10 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr |
| 14945 | 15065 | const field_name = try sema.resolveConstString(block, name_src, extra.field_name); |
| 14946 | 15066 | const field_ptr = sema.resolveInst(extra.field_ptr); |
| 14947 | 15067 | const field_ptr_ty = sema.typeOf(field_ptr); |
| 15068 | const target = sema.mod.getTarget(); | |
| 14948 | 15069 | |
| 14949 | 15070 | if (struct_ty.zigTypeTag() != .Struct) { |
| 14950 | return sema.fail(block, ty_src, "expected struct type, found '{}'", .{struct_ty}); | |
| 15071 | return sema.fail(block, ty_src, "expected struct type, found '{}'", .{struct_ty.fmt(target)}); | |
| 14951 | 15072 | } |
| 14952 | 15073 | try sema.resolveTypeLayout(block, ty_src, struct_ty); |
| 14953 | 15074 | |
| ... | ... | @@ -14956,7 +15077,7 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr |
| 14956 | 15077 | return sema.failWithBadStructFieldAccess(block, struct_obj, name_src, field_name); |
| 14957 | 15078 | |
| 14958 | 15079 | if (field_ptr_ty.zigTypeTag() != .Pointer) { |
| 14959 | return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{field_ptr_ty}); | |
| 15080 | return sema.fail(block, ty_src, "expected pointer type, found '{}'", .{field_ptr_ty.fmt(target)}); | |
| 14960 | 15081 | } |
| 14961 | 15082 | const field = struct_obj.fields.values()[field_index]; |
| 14962 | 15083 | const field_ptr_ty_info = field_ptr_ty.ptrInfo().data; |
| ... | ... | @@ -14973,7 +15094,6 @@ fn zirFieldParentPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileEr |
| 14973 | 15094 | ptr_ty_data.@"align" = field.abi_align; |
| 14974 | 15095 | } |
| 14975 | 15096 | |
| 14976 | const target = sema.mod.getTarget(); | |
| 14977 | 15097 | const actual_field_ptr_ty = try Type.ptr(sema.arena, target, ptr_ty_data); |
| 14978 | 15098 | const casted_field_ptr = try sema.coerce(block, actual_field_ptr_ty, field_ptr, ptr_src); |
| 14979 | 15099 | |
| ... | ... | @@ -15042,8 +15162,9 @@ fn analyzeMinMax( |
| 15042 | 15162 | .max => Value.numberMax, |
| 15043 | 15163 | else => unreachable, |
| 15044 | 15164 | }; |
| 15165 | const target = sema.mod.getTarget(); | |
| 15045 | 15166 | const vec_len = simd_op.len orelse { |
| 15046 | const result_val = opFunc(lhs_val, rhs_val); | |
| 15167 | const result_val = opFunc(lhs_val, rhs_val, target); | |
| 15047 | 15168 | return sema.addConstant(simd_op.result_ty, result_val); |
| 15048 | 15169 | }; |
| 15049 | 15170 | var lhs_buf: Value.ElemValueBuffer = undefined; |
| ... | ... | @@ -15052,7 +15173,7 @@ fn analyzeMinMax( |
| 15052 | 15173 | for (elems) |*elem, i| { |
| 15053 | 15174 | const lhs_elem_val = lhs_val.elemValueBuffer(i, &lhs_buf); |
| 15054 | 15175 | const rhs_elem_val = rhs_val.elemValueBuffer(i, &rhs_buf); |
| 15055 | elem.* = opFunc(lhs_elem_val, rhs_elem_val); | |
| 15176 | elem.* = opFunc(lhs_elem_val, rhs_elem_val, target); | |
| 15056 | 15177 | } |
| 15057 | 15178 | return sema.addConstant( |
| 15058 | 15179 | simd_op.result_ty, |
| ... | ... | @@ -15078,17 +15199,17 @@ fn zirMemcpy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 15078 | 15199 | const len_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = inst_data.src_node }; |
| 15079 | 15200 | const dest_ptr = sema.resolveInst(extra.dest); |
| 15080 | 15201 | const dest_ptr_ty = sema.typeOf(dest_ptr); |
| 15202 | const target = sema.mod.getTarget(); | |
| 15081 | 15203 | |
| 15082 | 15204 | try sema.checkPtrOperand(block, dest_src, dest_ptr_ty); |
| 15083 | 15205 | if (dest_ptr_ty.isConstPtr()) { |
| 15084 | return sema.fail(block, dest_src, "cannot store through const pointer '{}'", .{dest_ptr_ty}); | |
| 15206 | return sema.fail(block, dest_src, "cannot store through const pointer '{}'", .{dest_ptr_ty.fmt(target)}); | |
| 15085 | 15207 | } |
| 15086 | 15208 | |
| 15087 | 15209 | const uncasted_src_ptr = sema.resolveInst(extra.source); |
| 15088 | 15210 | const uncasted_src_ptr_ty = sema.typeOf(uncasted_src_ptr); |
| 15089 | 15211 | try sema.checkPtrOperand(block, src_src, uncasted_src_ptr_ty); |
| 15090 | 15212 | const src_ptr_info = uncasted_src_ptr_ty.ptrInfo().data; |
| 15091 | const target = sema.mod.getTarget(); | |
| 15092 | 15213 | const wanted_src_ptr_ty = try Type.ptr(sema.arena, target, .{ |
| 15093 | 15214 | .pointee_type = dest_ptr_ty.elemType2(), |
| 15094 | 15215 | .@"align" = src_ptr_info.@"align", |
| ... | ... | @@ -15136,9 +15257,10 @@ fn zirMemset(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!void |
| 15136 | 15257 | const len_src: LazySrcLoc = .{ .node_offset_builtin_call_arg2 = inst_data.src_node }; |
| 15137 | 15258 | const dest_ptr = sema.resolveInst(extra.dest); |
| 15138 | 15259 | const dest_ptr_ty = sema.typeOf(dest_ptr); |
| 15260 | const target = sema.mod.getTarget(); | |
| 15139 | 15261 | try sema.checkPtrOperand(block, dest_src, dest_ptr_ty); |
| 15140 | 15262 | if (dest_ptr_ty.isConstPtr()) { |
| 15141 | return sema.fail(block, dest_src, "cannot store through const pointer '{}'", .{dest_ptr_ty}); | |
| 15263 | return sema.fail(block, dest_src, "cannot store through const pointer '{}'", .{dest_ptr_ty.fmt(target)}); | |
| 15142 | 15264 | } |
| 15143 | 15265 | const elem_ty = dest_ptr_ty.elemType2(); |
| 15144 | 15266 | const value = try sema.coerce(block, elem_ty, sema.resolveInst(extra.byte), value_src); |
| ... | ... | @@ -15452,6 +15574,7 @@ fn zirPrefetch( |
| 15452 | 15574 | const ptr = sema.resolveInst(extra.lhs); |
| 15453 | 15575 | try sema.checkPtrOperand(block, ptr_src, sema.typeOf(ptr)); |
| 15454 | 15576 | const options = try sema.coerce(block, options_ty, sema.resolveInst(extra.rhs), opts_src); |
| 15577 | const target = sema.mod.getTarget(); | |
| 15455 | 15578 | |
| 15456 | 15579 | const rw = try sema.fieldVal(block, opts_src, options, "rw", opts_src); |
| 15457 | 15580 | const rw_val = try sema.resolveConstValue(block, opts_src, rw); |
| ... | ... | @@ -15459,7 +15582,7 @@ fn zirPrefetch( |
| 15459 | 15582 | |
| 15460 | 15583 | const locality = try sema.fieldVal(block, opts_src, options, "locality", opts_src); |
| 15461 | 15584 | const locality_val = try sema.resolveConstValue(block, opts_src, locality); |
| 15462 | const locality_int = @intCast(u2, locality_val.toUnsignedInt()); | |
| 15585 | const locality_int = @intCast(u2, locality_val.toUnsignedInt(target)); | |
| 15463 | 15586 | |
| 15464 | 15587 | const cache = try sema.fieldVal(block, opts_src, options, "cache", opts_src); |
| 15465 | 15588 | const cache_val = try sema.resolveConstValue(block, opts_src, cache); |
| ... | ... | @@ -15492,6 +15615,7 @@ fn zirBuiltinExtern( |
| 15492 | 15615 | |
| 15493 | 15616 | var ty = try sema.resolveType(block, ty_src, extra.lhs); |
| 15494 | 15617 | const options_inst = sema.resolveInst(extra.rhs); |
| 15618 | const target = sema.mod.getTarget(); | |
| 15495 | 15619 | |
| 15496 | 15620 | const options = options: { |
| 15497 | 15621 | const extern_options_ty = try sema.getBuiltinType(block, options_src, "ExternOptions"); |
| ... | ... | @@ -15512,11 +15636,11 @@ fn zirBuiltinExtern( |
| 15512 | 15636 | var library_name: ?[]const u8 = null; |
| 15513 | 15637 | if (!library_name_val.isNull()) { |
| 15514 | 15638 | const payload = library_name_val.castTag(.opt_payload).?.data; |
| 15515 | library_name = try payload.toAllocatedBytes(Type.initTag(.const_slice_u8), sema.arena); | |
| 15639 | library_name = try payload.toAllocatedBytes(Type.initTag(.const_slice_u8), sema.arena, target); | |
| 15516 | 15640 | } |
| 15517 | 15641 | |
| 15518 | 15642 | break :options std.builtin.ExternOptions{ |
| 15519 | .name = try name_val.toAllocatedBytes(Type.initTag(.const_slice_u8), sema.arena), | |
| 15643 | .name = try name_val.toAllocatedBytes(Type.initTag(.const_slice_u8), sema.arena, target), | |
| 15520 | 15644 | .library_name = library_name, |
| 15521 | 15645 | .linkage = linkage_val.toEnum(std.builtin.GlobalLinkage), |
| 15522 | 15646 | .is_thread_local = is_thread_local_val.toBool(), |
| ... | ... | @@ -15609,8 +15733,9 @@ fn validateVarType( |
| 15609 | 15733 | ) CompileError!void { |
| 15610 | 15734 | if (try sema.validateRunTimeType(block, src, var_ty, is_extern)) return; |
| 15611 | 15735 | |
| 15736 | const target = sema.mod.getTarget(); | |
| 15612 | 15737 | const msg = msg: { |
| 15613 | const msg = try sema.errMsg(block, src, "variable of type '{}' must be const or comptime", .{var_ty}); | |
| 15738 | const msg = try sema.errMsg(block, src, "variable of type '{}' must be const or comptime", .{var_ty.fmt(target)}); | |
| 15614 | 15739 | errdefer msg.destroy(sema.gpa); |
| 15615 | 15740 | |
| 15616 | 15741 | try sema.explainWhyTypeIsComptime(block, src, msg, src.toSrcLoc(block.src_decl), var_ty); |
| ... | ... | @@ -15685,6 +15810,7 @@ fn explainWhyTypeIsComptime( |
| 15685 | 15810 | ty: Type, |
| 15686 | 15811 | ) CompileError!void { |
| 15687 | 15812 | const mod = sema.mod; |
| 15813 | const target = mod.getTarget(); | |
| 15688 | 15814 | switch (ty.zigTypeTag()) { |
| 15689 | 15815 | .Bool, |
| 15690 | 15816 | .Int, |
| ... | ... | @@ -15698,7 +15824,7 @@ fn explainWhyTypeIsComptime( |
| 15698 | 15824 | |
| 15699 | 15825 | .Fn => { |
| 15700 | 15826 | try mod.errNoteNonLazy(src_loc, msg, "use '*const {}' for a function pointer type", .{ |
| 15701 | ty, | |
| 15827 | ty.fmt(target), | |
| 15702 | 15828 | }); |
| 15703 | 15829 | }, |
| 15704 | 15830 | |
| ... | ... | @@ -15941,6 +16067,8 @@ fn fieldVal( |
| 15941 | 16067 | else |
| 15942 | 16068 | object_ty; |
| 15943 | 16069 | |
| 16070 | const target = sema.mod.getTarget(); | |
| 16071 | ||
| 15944 | 16072 | switch (inner_ty.zigTypeTag()) { |
| 15945 | 16073 | .Array => { |
| 15946 | 16074 | if (mem.eql(u8, field_name, "len")) { |
| ... | ... | @@ -15953,7 +16081,7 @@ fn fieldVal( |
| 15953 | 16081 | block, |
| 15954 | 16082 | field_name_src, |
| 15955 | 16083 | "no member named '{s}' in '{}'", |
| 15956 | .{ field_name, object_ty }, | |
| 16084 | .{ field_name, object_ty.fmt(target) }, | |
| 15957 | 16085 | ); |
| 15958 | 16086 | } |
| 15959 | 16087 | }, |
| ... | ... | @@ -15977,7 +16105,7 @@ fn fieldVal( |
| 15977 | 16105 | block, |
| 15978 | 16106 | field_name_src, |
| 15979 | 16107 | "no member named '{s}' in '{}'", |
| 15980 | .{ field_name, object_ty }, | |
| 16108 | .{ field_name, object_ty.fmt(target) }, | |
| 15981 | 16109 | ); |
| 15982 | 16110 | } |
| 15983 | 16111 | } else if (ptr_info.pointee_type.zigTypeTag() == .Array) { |
| ... | ... | @@ -15991,7 +16119,7 @@ fn fieldVal( |
| 15991 | 16119 | block, |
| 15992 | 16120 | field_name_src, |
| 15993 | 16121 | "no member named '{s}' in '{}'", |
| 15994 | .{ field_name, ptr_info.pointee_type }, | |
| 16122 | .{ field_name, ptr_info.pointee_type.fmt(target) }, | |
| 15995 | 16123 | ); |
| 15996 | 16124 | } |
| 15997 | 16125 | } |
| ... | ... | @@ -16013,7 +16141,7 @@ fn fieldVal( |
| 16013 | 16141 | break :blk entry.key_ptr.*; |
| 16014 | 16142 | } |
| 16015 | 16143 | return sema.fail(block, src, "no error named '{s}' in '{}'", .{ |
| 16016 | field_name, child_type, | |
| 16144 | field_name, child_type.fmt(target), | |
| 16017 | 16145 | }); |
| 16018 | 16146 | } else (try sema.mod.getErrorValue(field_name)).key; |
| 16019 | 16147 | |
| ... | ... | @@ -16067,10 +16195,10 @@ fn fieldVal( |
| 16067 | 16195 | else => unreachable, |
| 16068 | 16196 | }; |
| 16069 | 16197 | return sema.fail(block, src, "{s} '{}' has no member named '{s}'", .{ |
| 16070 | kw_name, child_type, field_name, | |
| 16198 | kw_name, child_type.fmt(target), field_name, | |
| 16071 | 16199 | }); |
| 16072 | 16200 | }, |
| 16073 | else => return sema.fail(block, src, "type '{}' has no members", .{child_type}), | |
| 16201 | else => return sema.fail(block, src, "type '{}' has no members", .{child_type.fmt(target)}), | |
| 16074 | 16202 | } |
| 16075 | 16203 | }, |
| 16076 | 16204 | .Struct => if (is_pointer_to) { |
| ... | ... | @@ -16089,7 +16217,7 @@ fn fieldVal( |
| 16089 | 16217 | }, |
| 16090 | 16218 | else => {}, |
| 16091 | 16219 | } |
| 16092 | return sema.fail(block, src, "type '{}' does not support field access", .{object_ty}); | |
| 16220 | return sema.fail(block, src, "type '{}' does not support field access", .{object_ty.fmt(target)}); | |
| 16093 | 16221 | } |
| 16094 | 16222 | |
| 16095 | 16223 | fn fieldPtr( |
| ... | ... | @@ -16103,11 +16231,12 @@ fn fieldPtr( |
| 16103 | 16231 | // When editing this function, note that there is corresponding logic to be edited |
| 16104 | 16232 | // in `fieldVal`. This function takes a pointer and returns a pointer. |
| 16105 | 16233 | |
| 16234 | const target = sema.mod.getTarget(); | |
| 16106 | 16235 | const object_ptr_src = src; // TODO better source location |
| 16107 | 16236 | const object_ptr_ty = sema.typeOf(object_ptr); |
| 16108 | 16237 | const object_ty = switch (object_ptr_ty.zigTypeTag()) { |
| 16109 | 16238 | .Pointer => object_ptr_ty.elemType(), |
| 16110 | else => return sema.fail(block, object_ptr_src, "expected pointer, found '{}'", .{object_ptr_ty}), | |
| 16239 | else => return sema.fail(block, object_ptr_src, "expected pointer, found '{}'", .{object_ptr_ty.fmt(target)}), | |
| 16111 | 16240 | }; |
| 16112 | 16241 | |
| 16113 | 16242 | // Zig allows dereferencing a single pointer during field lookup. Note that |
| ... | ... | @@ -16120,8 +16249,6 @@ fn fieldPtr( |
| 16120 | 16249 | else |
| 16121 | 16250 | object_ty; |
| 16122 | 16251 | |
| 16123 | const target = sema.mod.getTarget(); | |
| 16124 | ||
| 16125 | 16252 | switch (inner_ty.zigTypeTag()) { |
| 16126 | 16253 | .Array => { |
| 16127 | 16254 | if (mem.eql(u8, field_name, "len")) { |
| ... | ... | @@ -16137,7 +16264,7 @@ fn fieldPtr( |
| 16137 | 16264 | block, |
| 16138 | 16265 | field_name_src, |
| 16139 | 16266 | "no member named '{s}' in '{}'", |
| 16140 | .{ field_name, object_ty }, | |
| 16267 | .{ field_name, object_ty.fmt(target) }, | |
| 16141 | 16268 | ); |
| 16142 | 16269 | } |
| 16143 | 16270 | }, |
| ... | ... | @@ -16177,7 +16304,7 @@ fn fieldPtr( |
| 16177 | 16304 | |
| 16178 | 16305 | return sema.analyzeDeclRef(try anon_decl.finish( |
| 16179 | 16306 | Type.usize, |
| 16180 | try Value.Tag.int_u64.create(anon_decl.arena(), val.sliceLen()), | |
| 16307 | try Value.Tag.int_u64.create(anon_decl.arena(), val.sliceLen(target)), | |
| 16181 | 16308 | 0, // default alignment |
| 16182 | 16309 | )); |
| 16183 | 16310 | } |
| ... | ... | @@ -16195,7 +16322,7 @@ fn fieldPtr( |
| 16195 | 16322 | block, |
| 16196 | 16323 | field_name_src, |
| 16197 | 16324 | "no member named '{s}' in '{}'", |
| 16198 | .{ field_name, object_ty }, | |
| 16325 | .{ field_name, object_ty.fmt(target) }, | |
| 16199 | 16326 | ); |
| 16200 | 16327 | } |
| 16201 | 16328 | }, |
| ... | ... | @@ -16219,7 +16346,7 @@ fn fieldPtr( |
| 16219 | 16346 | break :blk entry.key_ptr.*; |
| 16220 | 16347 | } |
| 16221 | 16348 | return sema.fail(block, src, "no error named '{s}' in '{}'", .{ |
| 16222 | field_name, child_type, | |
| 16349 | field_name, child_type.fmt(target), | |
| 16223 | 16350 | }); |
| 16224 | 16351 | } else (try sema.mod.getErrorValue(field_name)).key; |
| 16225 | 16352 | |
| ... | ... | @@ -16277,7 +16404,7 @@ fn fieldPtr( |
| 16277 | 16404 | } |
| 16278 | 16405 | return sema.failWithBadMemberAccess(block, child_type, field_name_src, field_name); |
| 16279 | 16406 | }, |
| 16280 | else => return sema.fail(block, src, "type '{}' has no members", .{child_type}), | |
| 16407 | else => return sema.fail(block, src, "type '{}' has no members", .{child_type.fmt(target)}), | |
| 16281 | 16408 | } |
| 16282 | 16409 | }, |
| 16283 | 16410 | .Struct => { |
| ... | ... | @@ -16296,7 +16423,7 @@ fn fieldPtr( |
| 16296 | 16423 | }, |
| 16297 | 16424 | else => {}, |
| 16298 | 16425 | } |
| 16299 | return sema.fail(block, src, "type '{}' does not support field access (fieldPtr, {}.{s})", .{ object_ty, object_ptr_ty, field_name }); | |
| 16426 | return sema.fail(block, src, "type '{}' does not support field access (fieldPtr, {}.{s})", .{ object_ty.fmt(target), object_ptr_ty.fmt(target), field_name }); | |
| 16300 | 16427 | } |
| 16301 | 16428 | |
| 16302 | 16429 | fn fieldCallBind( |
| ... | ... | @@ -16310,12 +16437,13 @@ fn fieldCallBind( |
| 16310 | 16437 | // When editing this function, note that there is corresponding logic to be edited |
| 16311 | 16438 | // in `fieldVal`. This function takes a pointer and returns a pointer. |
| 16312 | 16439 | |
| 16440 | const target = sema.mod.getTarget(); | |
| 16313 | 16441 | const raw_ptr_src = src; // TODO better source location |
| 16314 | 16442 | const raw_ptr_ty = sema.typeOf(raw_ptr); |
| 16315 | 16443 | const inner_ty = if (raw_ptr_ty.zigTypeTag() == .Pointer and raw_ptr_ty.ptrSize() == .One) |
| 16316 | 16444 | raw_ptr_ty.childType() |
| 16317 | 16445 | else |
| 16318 | return sema.fail(block, raw_ptr_src, "expected single pointer, found '{}'", .{raw_ptr_ty}); | |
| 16446 | return sema.fail(block, raw_ptr_src, "expected single pointer, found '{}'", .{raw_ptr_ty.fmt(target)}); | |
| 16319 | 16447 | |
| 16320 | 16448 | // Optionally dereference a second pointer to get the concrete type. |
| 16321 | 16449 | const is_double_ptr = inner_ty.zigTypeTag() == .Pointer and inner_ty.ptrSize() == .One; |
| ... | ... | @@ -16375,7 +16503,7 @@ fn fieldCallBind( |
| 16375 | 16503 | first_param_type.zigTypeTag() == .Pointer and |
| 16376 | 16504 | (first_param_type.ptrSize() == .One or |
| 16377 | 16505 | first_param_type.ptrSize() == .C) and |
| 16378 | first_param_type.childType().eql(concrete_ty))) | |
| 16506 | first_param_type.childType().eql(concrete_ty, target))) | |
| 16379 | 16507 | { |
| 16380 | 16508 | // zig fmt: on |
| 16381 | 16509 | // TODO: bound fn calls on rvalues should probably |
| ... | ... | @@ -16386,7 +16514,7 @@ fn fieldCallBind( |
| 16386 | 16514 | .arg0_inst = object_ptr, |
| 16387 | 16515 | }); |
| 16388 | 16516 | return sema.addConstant(ty, value); |
| 16389 | } else if (first_param_type.eql(concrete_ty)) { | |
| 16517 | } else if (first_param_type.eql(concrete_ty, target)) { | |
| 16390 | 16518 | var deref = try sema.analyzeLoad(block, src, object_ptr, src); |
| 16391 | 16519 | const ty = Type.Tag.bound_fn.init(); |
| 16392 | 16520 | const value = try Value.Tag.bound_fn.create(arena, .{ |
| ... | ... | @@ -16402,7 +16530,7 @@ fn fieldCallBind( |
| 16402 | 16530 | else => {}, |
| 16403 | 16531 | } |
| 16404 | 16532 | |
| 16405 | return sema.fail(block, src, "type '{}' has no field or member function named '{s}'", .{ concrete_ty, field_name }); | |
| 16533 | return sema.fail(block, src, "type '{}' has no field or member function named '{s}'", .{ concrete_ty.fmt(target), field_name }); | |
| 16406 | 16534 | } |
| 16407 | 16535 | |
| 16408 | 16536 | fn finishFieldCallBind( |
| ... | ... | @@ -16540,10 +16668,11 @@ fn structFieldPtrByIndex( |
| 16540 | 16668 | .@"addrspace" = struct_ptr_ty_info.@"addrspace", |
| 16541 | 16669 | }; |
| 16542 | 16670 | |
| 16671 | const target = sema.mod.getTarget(); | |
| 16672 | ||
| 16543 | 16673 | // TODO handle when the struct pointer is overaligned, we should return a potentially |
| 16544 | 16674 | // over-aligned field pointer too. |
| 16545 | 16675 | if (struct_obj.layout == .Packed) { |
| 16546 | const target = sema.mod.getTarget(); | |
| 16547 | 16676 | comptime assert(Type.packed_struct_layout_version == 2); |
| 16548 | 16677 | |
| 16549 | 16678 | var running_bits: u16 = 0; |
| ... | ... | @@ -16567,7 +16696,6 @@ fn structFieldPtrByIndex( |
| 16567 | 16696 | ptr_ty_data.@"align" = field.abi_align; |
| 16568 | 16697 | } |
| 16569 | 16698 | |
| 16570 | const target = sema.mod.getTarget(); | |
| 16571 | 16699 | const ptr_field_ty = try Type.ptr(sema.arena, target, ptr_ty_data); |
| 16572 | 16700 | |
| 16573 | 16701 | if (field.is_comptime) { |
| ... | ... | @@ -16667,14 +16795,15 @@ fn tupleFieldIndex( |
| 16667 | 16795 | field_name: []const u8, |
| 16668 | 16796 | field_name_src: LazySrcLoc, |
| 16669 | 16797 | ) CompileError!u32 { |
| 16798 | const target = sema.mod.getTarget(); | |
| 16670 | 16799 | const field_index = std.fmt.parseUnsigned(u32, field_name, 10) catch |err| { |
| 16671 | 16800 | return sema.fail(block, field_name_src, "tuple {} has no such field '{s}': {s}", .{ |
| 16672 | tuple_ty, field_name, @errorName(err), | |
| 16801 | tuple_ty.fmt(target), field_name, @errorName(err), | |
| 16673 | 16802 | }); |
| 16674 | 16803 | }; |
| 16675 | 16804 | if (field_index >= tuple_ty.structFieldCount()) { |
| 16676 | 16805 | return sema.fail(block, field_name_src, "tuple {} has no such field '{s}'", .{ |
| 16677 | tuple_ty, field_name, | |
| 16806 | tuple_ty.fmt(target), field_name, | |
| 16678 | 16807 | }); |
| 16679 | 16808 | } |
| 16680 | 16809 | return field_index; |
| ... | ... | @@ -16749,7 +16878,7 @@ fn unionFieldPtr( |
| 16749 | 16878 | // .data = field_index, |
| 16750 | 16879 | //}; |
| 16751 | 16880 | //const field_tag = Value.initPayload(&field_tag_buf.base); |
| 16752 | //const tag_matches = tag_and_val.tag.eql(field_tag, union_obj.tag_ty); | |
| 16881 | //const tag_matches = tag_and_val.tag.eql(field_tag, union_obj.tag_ty, target); | |
| 16753 | 16882 | //if (!tag_matches) { |
| 16754 | 16883 | // // TODO enhance this saying which one was active |
| 16755 | 16884 | // // and which one was accessed, and showing where the union was declared. |
| ... | ... | @@ -16798,7 +16927,8 @@ fn unionFieldVal( |
| 16798 | 16927 | .data = field_index, |
| 16799 | 16928 | }; |
| 16800 | 16929 | const field_tag = Value.initPayload(&field_tag_buf.base); |
| 16801 | const tag_matches = tag_and_val.tag.eql(field_tag, union_obj.tag_ty); | |
| 16930 | const target = sema.mod.getTarget(); | |
| 16931 | const tag_matches = tag_and_val.tag.eql(field_tag, union_obj.tag_ty, target); | |
| 16802 | 16932 | switch (union_obj.layout) { |
| 16803 | 16933 | .Auto => { |
| 16804 | 16934 | if (tag_matches) { |
| ... | ... | @@ -16813,7 +16943,7 @@ fn unionFieldVal( |
| 16813 | 16943 | if (tag_matches) { |
| 16814 | 16944 | return sema.addConstant(field.ty, tag_and_val.val); |
| 16815 | 16945 | } else { |
| 16816 | const old_ty = union_ty.unionFieldType(tag_and_val.tag); | |
| 16946 | const old_ty = union_ty.unionFieldType(tag_and_val.tag, target); | |
| 16817 | 16947 | const new_val = try sema.bitCastVal(block, src, tag_and_val.val, old_ty, field.ty, 0); |
| 16818 | 16948 | return sema.addConstant(field.ty, new_val); |
| 16819 | 16949 | } |
| ... | ... | @@ -16835,19 +16965,19 @@ fn elemPtr( |
| 16835 | 16965 | ) CompileError!Air.Inst.Ref { |
| 16836 | 16966 | const indexable_ptr_src = src; // TODO better source location |
| 16837 | 16967 | const indexable_ptr_ty = sema.typeOf(indexable_ptr); |
| 16968 | const target = sema.mod.getTarget(); | |
| 16838 | 16969 | const indexable_ty = switch (indexable_ptr_ty.zigTypeTag()) { |
| 16839 | 16970 | .Pointer => indexable_ptr_ty.elemType(), |
| 16840 | else => return sema.fail(block, indexable_ptr_src, "expected pointer, found '{}'", .{indexable_ptr_ty}), | |
| 16971 | else => return sema.fail(block, indexable_ptr_src, "expected pointer, found '{}'", .{indexable_ptr_ty.fmt(target)}), | |
| 16841 | 16972 | }; |
| 16842 | 16973 | if (!indexable_ty.isIndexable()) { |
| 16843 | return sema.fail(block, src, "element access of non-indexable type '{}'", .{indexable_ty}); | |
| 16974 | return sema.fail(block, src, "element access of non-indexable type '{}'", .{indexable_ty.fmt(target)}); | |
| 16844 | 16975 | } |
| 16845 | 16976 | |
| 16846 | 16977 | switch (indexable_ty.zigTypeTag()) { |
| 16847 | 16978 | .Pointer => { |
| 16848 | 16979 | // In all below cases, we have to deref the ptr operand to get the actual indexable pointer. |
| 16849 | 16980 | const indexable = try sema.analyzeLoad(block, indexable_ptr_src, indexable_ptr, indexable_ptr_src); |
| 16850 | const target = sema.mod.getTarget(); | |
| 16851 | 16981 | const result_ty = try indexable_ty.elemPtrType(sema.arena, target); |
| 16852 | 16982 | switch (indexable_ty.ptrSize()) { |
| 16853 | 16983 | .Slice => return sema.elemPtrSlice(block, indexable_ptr_src, indexable, elem_index_src, elem_index), |
| ... | ... | @@ -16858,8 +16988,8 @@ fn elemPtr( |
| 16858 | 16988 | const runtime_src = rs: { |
| 16859 | 16989 | const ptr_val = maybe_ptr_val orelse break :rs indexable_ptr_src; |
| 16860 | 16990 | const index_val = maybe_index_val orelse break :rs elem_index_src; |
| 16861 | const index = @intCast(usize, index_val.toUnsignedInt()); | |
| 16862 | const elem_ptr = try ptr_val.elemPtr(indexable_ty, sema.arena, index); | |
| 16991 | const index = @intCast(usize, index_val.toUnsignedInt(target)); | |
| 16992 | const elem_ptr = try ptr_val.elemPtr(indexable_ty, sema.arena, index, target); | |
| 16863 | 16993 | return sema.addConstant(result_ty, elem_ptr); |
| 16864 | 16994 | }; |
| 16865 | 16995 | |
| ... | ... | @@ -16876,7 +17006,7 @@ fn elemPtr( |
| 16876 | 17006 | .Struct => { |
| 16877 | 17007 | // Tuple field access. |
| 16878 | 17008 | const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index); |
| 16879 | const index = @intCast(u32, index_val.toUnsignedInt()); | |
| 17009 | const index = @intCast(u32, index_val.toUnsignedInt(target)); | |
| 16880 | 17010 | return sema.tupleFieldPtr(block, src, indexable_ptr, elem_index_src, index); |
| 16881 | 17011 | }, |
| 16882 | 17012 | else => unreachable, |
| ... | ... | @@ -16893,9 +17023,10 @@ fn elemVal( |
| 16893 | 17023 | ) CompileError!Air.Inst.Ref { |
| 16894 | 17024 | const indexable_src = src; // TODO better source location |
| 16895 | 17025 | const indexable_ty = sema.typeOf(indexable); |
| 17026 | const target = sema.mod.getTarget(); | |
| 16896 | 17027 | |
| 16897 | 17028 | if (!indexable_ty.isIndexable()) { |
| 16898 | return sema.fail(block, src, "element access of non-indexable type '{}'", .{indexable_ty}); | |
| 17029 | return sema.fail(block, src, "element access of non-indexable type '{}'", .{indexable_ty.fmt(target)}); | |
| 16899 | 17030 | } |
| 16900 | 17031 | |
| 16901 | 17032 | // TODO in case of a vector of pointers, we need to detect whether the element |
| ... | ... | @@ -16912,7 +17043,7 @@ fn elemVal( |
| 16912 | 17043 | const runtime_src = rs: { |
| 16913 | 17044 | const indexable_val = maybe_indexable_val orelse break :rs indexable_src; |
| 16914 | 17045 | const index_val = maybe_index_val orelse break :rs elem_index_src; |
| 16915 | const index = @intCast(usize, index_val.toUnsignedInt()); | |
| 17046 | const index = @intCast(usize, index_val.toUnsignedInt(target)); | |
| 16916 | 17047 | const elem_ty = indexable_ty.elemType2(); |
| 16917 | 17048 | |
| 16918 | 17049 | var payload: Value.Payload.ElemPtr = .{ .data = .{ |
| ... | ... | @@ -16945,7 +17076,7 @@ fn elemVal( |
| 16945 | 17076 | .Struct => { |
| 16946 | 17077 | // Tuple field access. |
| 16947 | 17078 | const index_val = try sema.resolveConstValue(block, elem_index_src, elem_index); |
| 16948 | const index = @intCast(u32, index_val.toUnsignedInt()); | |
| 17079 | const index = @intCast(u32, index_val.toUnsignedInt(target)); | |
| 16949 | 17080 | return tupleField(sema, block, indexable_src, indexable, elem_index_src, index); |
| 16950 | 17081 | }, |
| 16951 | 17082 | else => unreachable, |
| ... | ... | @@ -17056,9 +17187,10 @@ fn elemValArray( |
| 17056 | 17187 | const maybe_undef_array_val = try sema.resolveMaybeUndefVal(block, array_src, array); |
| 17057 | 17188 | // index must be defined since it can access out of bounds |
| 17058 | 17189 | const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index); |
| 17190 | const target = sema.mod.getTarget(); | |
| 17059 | 17191 | |
| 17060 | 17192 | if (maybe_index_val) |index_val| { |
| 17061 | const index = @intCast(usize, index_val.toUnsignedInt()); | |
| 17193 | const index = @intCast(usize, index_val.toUnsignedInt(target)); | |
| 17062 | 17194 | if (index >= array_len_s) { |
| 17063 | 17195 | const sentinel_label: []const u8 = if (array_sent) " +1 (sentinel)" else ""; |
| 17064 | 17196 | return sema.fail(block, elem_index_src, "index {d} outside array of length {d}{s}", .{ index, array_len, sentinel_label }); |
| ... | ... | @@ -17069,7 +17201,7 @@ fn elemValArray( |
| 17069 | 17201 | return sema.addConstUndef(elem_ty); |
| 17070 | 17202 | } |
| 17071 | 17203 | if (maybe_index_val) |index_val| { |
| 17072 | const index = @intCast(usize, index_val.toUnsignedInt()); | |
| 17204 | const index = @intCast(usize, index_val.toUnsignedInt(target)); | |
| 17073 | 17205 | const elem_val = try array_val.elemValue(sema.arena, index); |
| 17074 | 17206 | return sema.addConstant(elem_ty, elem_val); |
| 17075 | 17207 | } |
| ... | ... | @@ -17114,7 +17246,7 @@ fn elemPtrArray( |
| 17114 | 17246 | const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index); |
| 17115 | 17247 | |
| 17116 | 17248 | if (maybe_index_val) |index_val| { |
| 17117 | const index = @intCast(usize, index_val.toUnsignedInt()); | |
| 17249 | const index = @intCast(usize, index_val.toUnsignedInt(target)); | |
| 17118 | 17250 | if (index >= array_len_s) { |
| 17119 | 17251 | const sentinel_label: []const u8 = if (array_sent) " +1 (sentinel)" else ""; |
| 17120 | 17252 | return sema.fail(block, elem_index_src, "index {d} outside array of length {d}{s}", .{ index, array_len, sentinel_label }); |
| ... | ... | @@ -17125,8 +17257,8 @@ fn elemPtrArray( |
| 17125 | 17257 | return sema.addConstUndef(elem_ptr_ty); |
| 17126 | 17258 | } |
| 17127 | 17259 | if (maybe_index_val) |index_val| { |
| 17128 | const index = @intCast(usize, index_val.toUnsignedInt()); | |
| 17129 | const elem_ptr = try array_ptr_val.elemPtr(array_ptr_ty, sema.arena, index); | |
| 17260 | const index = @intCast(usize, index_val.toUnsignedInt(target)); | |
| 17261 | const elem_ptr = try array_ptr_val.elemPtr(array_ptr_ty, sema.arena, index, target); | |
| 17130 | 17262 | return sema.addConstant(elem_ptr_ty, elem_ptr); |
| 17131 | 17263 | } |
| 17132 | 17264 | } |
| ... | ... | @@ -17162,16 +17294,17 @@ fn elemValSlice( |
| 17162 | 17294 | const maybe_slice_val = try sema.resolveDefinedValue(block, slice_src, slice); |
| 17163 | 17295 | // index must be defined since it can index out of bounds |
| 17164 | 17296 | const maybe_index_val = try sema.resolveDefinedValue(block, elem_index_src, elem_index); |
| 17297 | const target = sema.mod.getTarget(); | |
| 17165 | 17298 | |
| 17166 | 17299 | if (maybe_slice_val) |slice_val| { |
| 17167 | 17300 | runtime_src = elem_index_src; |
| 17168 | const slice_len = slice_val.sliceLen(); | |
| 17301 | const slice_len = slice_val.sliceLen(target); | |
| 17169 | 17302 | const slice_len_s = slice_len + @boolToInt(slice_sent); |
| 17170 | 17303 | if (slice_len_s == 0) { |
| 17171 | 17304 | return sema.fail(block, elem_index_src, "indexing into empty slice", .{}); |
| 17172 | 17305 | } |
| 17173 | 17306 | if (maybe_index_val) |index_val| { |
| 17174 | const index = @intCast(usize, index_val.toUnsignedInt()); | |
| 17307 | const index = @intCast(usize, index_val.toUnsignedInt(target)); | |
| 17175 | 17308 | if (index >= slice_len_s) { |
| 17176 | 17309 | const sentinel_label: []const u8 = if (slice_sent) " +1 (sentinel)" else ""; |
| 17177 | 17310 | return sema.fail(block, elem_index_src, "index {d} outside slice of length {d}{s}", .{ index, slice_len, sentinel_label }); |
| ... | ... | @@ -17192,7 +17325,7 @@ fn elemValSlice( |
| 17192 | 17325 | try sema.requireRuntimeBlock(block, runtime_src); |
| 17193 | 17326 | if (block.wantSafety()) { |
| 17194 | 17327 | const len_inst = if (maybe_slice_val) |slice_val| |
| 17195 | try sema.addIntUnsigned(Type.usize, slice_val.sliceLen()) | |
| 17328 | try sema.addIntUnsigned(Type.usize, slice_val.sliceLen(target)) | |
| 17196 | 17329 | else |
| 17197 | 17330 | try block.addTyOp(.slice_len, Type.usize, slice); |
| 17198 | 17331 | const cmp_op: Air.Inst.Tag = if (slice_sent) .cmp_lte else .cmp_lt; |
| ... | ... | @@ -17223,18 +17356,18 @@ fn elemPtrSlice( |
| 17223 | 17356 | if (slice_val.isUndef()) { |
| 17224 | 17357 | return sema.addConstUndef(elem_ptr_ty); |
| 17225 | 17358 | } |
| 17226 | const slice_len = slice_val.sliceLen(); | |
| 17359 | const slice_len = slice_val.sliceLen(target); | |
| 17227 | 17360 | const slice_len_s = slice_len + @boolToInt(slice_sent); |
| 17228 | 17361 | if (slice_len_s == 0) { |
| 17229 | 17362 | return sema.fail(block, elem_index_src, "indexing into empty slice", .{}); |
| 17230 | 17363 | } |
| 17231 | 17364 | if (maybe_index_val) |index_val| { |
| 17232 | const index = @intCast(usize, index_val.toUnsignedInt()); | |
| 17365 | const index = @intCast(usize, index_val.toUnsignedInt(target)); | |
| 17233 | 17366 | if (index >= slice_len_s) { |
| 17234 | 17367 | const sentinel_label: []const u8 = if (slice_sent) " +1 (sentinel)" else ""; |
| 17235 | 17368 | return sema.fail(block, elem_index_src, "index {d} outside slice of length {d}{s}", .{ index, slice_len, sentinel_label }); |
| 17236 | 17369 | } |
| 17237 | const elem_ptr_val = try slice_val.elemPtr(slice_ty, sema.arena, index); | |
| 17370 | const elem_ptr_val = try slice_val.elemPtr(slice_ty, sema.arena, index, target); | |
| 17238 | 17371 | return sema.addConstant(elem_ptr_ty, elem_ptr_val); |
| 17239 | 17372 | } |
| 17240 | 17373 | } |
| ... | ... | @@ -17245,7 +17378,7 @@ fn elemPtrSlice( |
| 17245 | 17378 | const len_inst = len: { |
| 17246 | 17379 | if (maybe_undef_slice_val) |slice_val| |
| 17247 | 17380 | if (!slice_val.isUndef()) |
| 17248 | break :len try sema.addIntUnsigned(Type.usize, slice_val.sliceLen()); | |
| 17381 | break :len try sema.addIntUnsigned(Type.usize, slice_val.sliceLen(target)); | |
| 17249 | 17382 | break :len try block.addTyOp(.slice_len, Type.usize, slice); |
| 17250 | 17383 | }; |
| 17251 | 17384 | const cmp_op: Air.Inst.Tag = if (slice_sent) .cmp_lte else .cmp_lt; |
| ... | ... | @@ -17270,12 +17403,12 @@ fn coerce( |
| 17270 | 17403 | const dest_ty_src = inst_src; // TODO better source location |
| 17271 | 17404 | const dest_ty = try sema.resolveTypeFields(block, dest_ty_src, dest_ty_unresolved); |
| 17272 | 17405 | const inst_ty = try sema.resolveTypeFields(block, inst_src, sema.typeOf(inst)); |
| 17406 | const target = sema.mod.getTarget(); | |
| 17273 | 17407 | // If the types are the same, we can return the operand. |
| 17274 | if (dest_ty.eql(inst_ty)) | |
| 17408 | if (dest_ty.eql(inst_ty, target)) | |
| 17275 | 17409 | return inst; |
| 17276 | 17410 | |
| 17277 | 17411 | const arena = sema.arena; |
| 17278 | const target = sema.mod.getTarget(); | |
| 17279 | 17412 | const maybe_inst_val = try sema.resolveMaybeUndefVal(block, inst_src, inst); |
| 17280 | 17413 | |
| 17281 | 17414 | const in_memory_result = try sema.coerceInMemoryAllowed(block, dest_ty, inst_ty, false, target, dest_ty_src, inst_src); |
| ... | ... | @@ -17379,7 +17512,7 @@ fn coerce( |
| 17379 | 17512 | // *[N:s]T to [*]T |
| 17380 | 17513 | if (dest_info.sentinel) |dst_sentinel| { |
| 17381 | 17514 | if (array_ty.sentinel()) |src_sentinel| { |
| 17382 | if (src_sentinel.eql(dst_sentinel, dst_elem_type)) { | |
| 17515 | if (src_sentinel.eql(dst_sentinel, dst_elem_type, target)) { | |
| 17383 | 17516 | return sema.coerceCompatiblePtrs(block, dest_ty, inst, inst_src); |
| 17384 | 17517 | } |
| 17385 | 17518 | } |
| ... | ... | @@ -17448,7 +17581,7 @@ fn coerce( |
| 17448 | 17581 | } |
| 17449 | 17582 | if (inst_info.size == .Slice) { |
| 17450 | 17583 | if (dest_info.sentinel == null or inst_info.sentinel == null or |
| 17451 | !dest_info.sentinel.?.eql(inst_info.sentinel.?, dest_info.pointee_type)) | |
| 17584 | !dest_info.sentinel.?.eql(inst_info.sentinel.?, dest_info.pointee_type, target)) | |
| 17452 | 17585 | break :p; |
| 17453 | 17586 | |
| 17454 | 17587 | const slice_ptr = try sema.analyzeSlicePtr(block, inst_src, inst, inst_ty); |
| ... | ... | @@ -17515,7 +17648,7 @@ fn coerce( |
| 17515 | 17648 | } |
| 17516 | 17649 | |
| 17517 | 17650 | if (dest_info.sentinel == null or inst_info.sentinel == null or |
| 17518 | !dest_info.sentinel.?.eql(inst_info.sentinel.?, dest_info.pointee_type)) | |
| 17651 | !dest_info.sentinel.?.eql(inst_info.sentinel.?, dest_info.pointee_type, target)) | |
| 17519 | 17652 | break :p; |
| 17520 | 17653 | |
| 17521 | 17654 | const slice_ptr = try sema.analyzeSlicePtr(block, inst_src, inst, inst_ty); |
| ... | ... | @@ -17528,11 +17661,11 @@ fn coerce( |
| 17528 | 17661 | const val = (try sema.resolveDefinedValue(block, inst_src, inst)) orelse break :float; |
| 17529 | 17662 | |
| 17530 | 17663 | if (val.floatHasFraction()) { |
| 17531 | return sema.fail(block, inst_src, "fractional component prevents float value {} from coercion to type '{}'", .{ val.fmtValue(inst_ty), dest_ty }); | |
| 17664 | return sema.fail(block, inst_src, "fractional component prevents float value {} from coercion to type '{}'", .{ val.fmtValue(inst_ty, target), dest_ty.fmt(target) }); | |
| 17532 | 17665 | } |
| 17533 | 17666 | const result_val = val.floatToInt(sema.arena, inst_ty, dest_ty, target) catch |err| switch (err) { |
| 17534 | 17667 | error.FloatCannotFit => { |
| 17535 | return sema.fail(block, inst_src, "integer value {d} cannot be stored in type '{}'", .{ std.math.floor(val.toFloat(f64)), dest_ty }); | |
| 17668 | return sema.fail(block, inst_src, "integer value {d} cannot be stored in type '{}'", .{ std.math.floor(val.toFloat(f64)), dest_ty.fmt(target) }); | |
| 17536 | 17669 | }, |
| 17537 | 17670 | else => |e| return e, |
| 17538 | 17671 | }; |
| ... | ... | @@ -17542,7 +17675,7 @@ fn coerce( |
| 17542 | 17675 | if (try sema.resolveDefinedValue(block, inst_src, inst)) |val| { |
| 17543 | 17676 | // comptime known integer to other number |
| 17544 | 17677 | if (!val.intFitsInType(dest_ty, target)) { |
| 17545 | return sema.fail(block, inst_src, "type {} cannot represent integer value {}", .{ dest_ty, val.fmtValue(inst_ty) }); | |
| 17678 | return sema.fail(block, inst_src, "type {} cannot represent integer value {}", .{ dest_ty.fmt(target), val.fmtValue(inst_ty, target) }); | |
| 17546 | 17679 | } |
| 17547 | 17680 | return try sema.addConstant(dest_ty, val); |
| 17548 | 17681 | } |
| ... | ... | @@ -17572,12 +17705,12 @@ fn coerce( |
| 17572 | 17705 | .Float => { |
| 17573 | 17706 | if (try sema.resolveDefinedValue(block, inst_src, inst)) |val| { |
| 17574 | 17707 | const result_val = try val.floatCast(sema.arena, dest_ty, target); |
| 17575 | if (!val.eql(result_val, dest_ty)) { | |
| 17708 | if (!val.eql(result_val, dest_ty, target)) { | |
| 17576 | 17709 | return sema.fail( |
| 17577 | 17710 | block, |
| 17578 | 17711 | inst_src, |
| 17579 | 17712 | "type {} cannot represent float value {}", |
| 17580 | .{ dest_ty, val.fmtValue(inst_ty) }, | |
| 17713 | .{ dest_ty.fmt(target), val.fmtValue(inst_ty, target) }, | |
| 17581 | 17714 | ); |
| 17582 | 17715 | } |
| 17583 | 17716 | return try sema.addConstant(dest_ty, result_val); |
| ... | ... | @@ -17596,12 +17729,12 @@ fn coerce( |
| 17596 | 17729 | const result_val = try val.intToFloat(sema.arena, inst_ty, dest_ty, target); |
| 17597 | 17730 | // TODO implement this compile error |
| 17598 | 17731 | //const int_again_val = try result_val.floatToInt(sema.arena, inst_ty); |
| 17599 | //if (!int_again_val.eql(val, inst_ty)) { | |
| 17732 | //if (!int_again_val.eql(val, inst_ty, target)) { | |
| 17600 | 17733 | // return sema.fail( |
| 17601 | 17734 | // block, |
| 17602 | 17735 | // inst_src, |
| 17603 | 17736 | // "type {} cannot represent integer value {}", |
| 17604 | // .{ dest_ty, val }, | |
| 17737 | // .{ dest_ty.fmt(target), val }, | |
| 17605 | 17738 | // ); |
| 17606 | 17739 | //} |
| 17607 | 17740 | return try sema.addConstant(dest_ty, result_val); |
| ... | ... | @@ -17622,7 +17755,7 @@ fn coerce( |
| 17622 | 17755 | block, |
| 17623 | 17756 | inst_src, |
| 17624 | 17757 | "enum '{}' has no field named '{s}'", |
| 17625 | .{ dest_ty, bytes }, | |
| 17758 | .{ dest_ty.fmt(target), bytes }, | |
| 17626 | 17759 | ); |
| 17627 | 17760 | errdefer msg.destroy(sema.gpa); |
| 17628 | 17761 | try sema.mod.errNoteNonLazy( |
| ... | ... | @@ -17643,7 +17776,7 @@ fn coerce( |
| 17643 | 17776 | .Union => blk: { |
| 17644 | 17777 | // union to its own tag type |
| 17645 | 17778 | const union_tag_ty = inst_ty.unionTagType() orelse break :blk; |
| 17646 | if (union_tag_ty.eql(dest_ty)) { | |
| 17779 | if (union_tag_ty.eql(dest_ty, target)) { | |
| 17647 | 17780 | return sema.unionToTag(block, dest_ty, inst, inst_src); |
| 17648 | 17781 | } |
| 17649 | 17782 | }, |
| ... | ... | @@ -17743,7 +17876,7 @@ fn coerce( |
| 17743 | 17876 | return sema.addConstUndef(dest_ty); |
| 17744 | 17877 | } |
| 17745 | 17878 | |
| 17746 | return sema.fail(block, inst_src, "expected {}, found {}", .{ dest_ty, inst_ty }); | |
| 17879 | return sema.fail(block, inst_src, "expected {}, found {}", .{ dest_ty.fmt(target), inst_ty.fmt(target) }); | |
| 17747 | 17880 | } |
| 17748 | 17881 | |
| 17749 | 17882 | const InMemoryCoercionResult = enum { |
| ... | ... | @@ -17772,7 +17905,7 @@ fn coerceInMemoryAllowed( |
| 17772 | 17905 | dest_src: LazySrcLoc, |
| 17773 | 17906 | src_src: LazySrcLoc, |
| 17774 | 17907 | ) CompileError!InMemoryCoercionResult { |
| 17775 | if (dest_ty.eql(src_ty)) | |
| 17908 | if (dest_ty.eql(src_ty, target)) | |
| 17776 | 17909 | return .ok; |
| 17777 | 17910 | |
| 17778 | 17911 | // Pointers / Pointer-like Optionals |
| ... | ... | @@ -17823,7 +17956,7 @@ fn coerceInMemoryAllowed( |
| 17823 | 17956 | } |
| 17824 | 17957 | const ok_sent = dest_info.sentinel == null or |
| 17825 | 17958 | (src_info.sentinel != null and |
| 17826 | dest_info.sentinel.?.eql(src_info.sentinel.?, dest_info.elem_type)); | |
| 17959 | dest_info.sentinel.?.eql(src_info.sentinel.?, dest_info.elem_type, target)); | |
| 17827 | 17960 | if (!ok_sent) { |
| 17828 | 17961 | return .no_match; |
| 17829 | 17962 | } |
| ... | ... | @@ -18050,7 +18183,7 @@ fn coerceInMemoryAllowedPtrs( |
| 18050 | 18183 | |
| 18051 | 18184 | const ok_sent = dest_info.sentinel == null or src_info.size == .C or |
| 18052 | 18185 | (src_info.sentinel != null and |
| 18053 | dest_info.sentinel.?.eql(src_info.sentinel.?, dest_info.pointee_type)); | |
| 18186 | dest_info.sentinel.?.eql(src_info.sentinel.?, dest_info.pointee_type, target)); | |
| 18054 | 18187 | if (!ok_sent) { |
| 18055 | 18188 | return .no_match; |
| 18056 | 18189 | } |
| ... | ... | @@ -18091,7 +18224,7 @@ fn coerceInMemoryAllowedPtrs( |
| 18091 | 18224 | // resolved and we compare the alignment numerically. |
| 18092 | 18225 | alignment: { |
| 18093 | 18226 | if (src_info.@"align" == 0 and dest_info.@"align" == 0 and |
| 18094 | dest_info.pointee_type.eql(src_info.pointee_type)) | |
| 18227 | dest_info.pointee_type.eql(src_info.pointee_type, target)) | |
| 18095 | 18228 | { |
| 18096 | 18229 | break :alignment; |
| 18097 | 18230 | } |
| ... | ... | @@ -18246,7 +18379,8 @@ fn obtainBitCastedVectorPtr(sema: *Sema, ptr: Air.Inst.Ref) ?Air.Inst.Ref { |
| 18246 | 18379 | // We have a pointer-to-array and a pointer-to-vector. If the elements and |
| 18247 | 18380 | // lengths match, return the result. |
| 18248 | 18381 | const vector_ty = sema.typeOf(prev_ptr).childType(); |
| 18249 | if (array_ty.childType().eql(vector_ty.childType()) and | |
| 18382 | const target = sema.mod.getTarget(); | |
| 18383 | if (array_ty.childType().eql(vector_ty.childType(), target) and | |
| 18250 | 18384 | array_ty.arrayLen() == vector_ty.vectorLen()) |
| 18251 | 18385 | { |
| 18252 | 18386 | return prev_ptr; |
| ... | ... | @@ -18265,15 +18399,15 @@ fn storePtrVal( |
| 18265 | 18399 | operand_val: Value, |
| 18266 | 18400 | operand_ty: Type, |
| 18267 | 18401 | ) !void { |
| 18268 | var kit = try beginComptimePtrMutation(sema, block, src, ptr_val); | |
| 18269 | try sema.checkComptimeVarStore(block, src, kit.decl_ref_mut); | |
| 18402 | var mut_kit = try beginComptimePtrMutation(sema, block, src, ptr_val); | |
| 18403 | try sema.checkComptimeVarStore(block, src, mut_kit.decl_ref_mut); | |
| 18270 | 18404 | |
| 18271 | const bitcasted_val = try sema.bitCastVal(block, src, operand_val, operand_ty, kit.ty, 0); | |
| 18405 | const bitcasted_val = try sema.bitCastVal(block, src, operand_val, operand_ty, mut_kit.ty, 0); | |
| 18272 | 18406 | |
| 18273 | const arena = kit.beginArena(sema.gpa); | |
| 18274 | defer kit.finishArena(); | |
| 18407 | const arena = mut_kit.beginArena(sema.gpa); | |
| 18408 | defer mut_kit.finishArena(); | |
| 18275 | 18409 | |
| 18276 | kit.val.* = try bitcasted_val.copy(arena); | |
| 18410 | mut_kit.val.* = try bitcasted_val.copy(arena); | |
| 18277 | 18411 | } |
| 18278 | 18412 | |
| 18279 | 18413 | const ComptimePtrMutationKit = struct { |
| ... | ... | @@ -18668,10 +18802,10 @@ fn beginComptimePtrLoad( |
| 18668 | 18802 | if (maybe_array_ty) |load_ty| { |
| 18669 | 18803 | // It's possible that we're loading a [N]T, in which case we'd like to slice |
| 18670 | 18804 | // the pointee array directly from our parent array. |
| 18671 | if (load_ty.isArrayLike() and load_ty.childType().eql(elem_ty)) { | |
| 18805 | if (load_ty.isArrayLike() and load_ty.childType().eql(elem_ty, target)) { | |
| 18672 | 18806 | const N = try sema.usizeCast(block, src, load_ty.arrayLenIncludingSentinel()); |
| 18673 | 18807 | deref.pointee = if (elem_ptr.index + N <= check_len) TypedValue{ |
| 18674 | .ty = try Type.array(sema.arena, N, null, elem_ty), | |
| 18808 | .ty = try Type.array(sema.arena, N, null, elem_ty, target), | |
| 18675 | 18809 | .val = try array_tv.val.sliceArray(sema.arena, elem_ptr.index, elem_ptr.index + N), |
| 18676 | 18810 | } else null; |
| 18677 | 18811 | break :blk deref; |
| ... | ... | @@ -18807,11 +18941,11 @@ pub fn bitCastVal( |
| 18807 | 18941 | new_ty: Type, |
| 18808 | 18942 | buffer_offset: usize, |
| 18809 | 18943 | ) !Value { |
| 18810 | if (old_ty.eql(new_ty)) return val; | |
| 18944 | const target = sema.mod.getTarget(); | |
| 18945 | if (old_ty.eql(new_ty, target)) return val; | |
| 18811 | 18946 | |
| 18812 | 18947 | // For types with well-defined memory layouts, we serialize them a byte buffer, |
| 18813 | 18948 | // then deserialize to the new type. |
| 18814 | const target = sema.mod.getTarget(); | |
| 18815 | 18949 | const abi_size = try sema.usizeCast(block, src, old_ty.abiSize(target)); |
| 18816 | 18950 | const buffer = try sema.gpa.alloc(u8, abi_size); |
| 18817 | 18951 | defer sema.gpa.free(buffer); |
| ... | ... | @@ -18864,11 +18998,12 @@ fn coerceEnumToUnion( |
| 18864 | 18998 | inst_src: LazySrcLoc, |
| 18865 | 18999 | ) !Air.Inst.Ref { |
| 18866 | 19000 | const inst_ty = sema.typeOf(inst); |
| 19001 | const target = sema.mod.getTarget(); | |
| 18867 | 19002 | |
| 18868 | 19003 | const tag_ty = union_ty.unionTagType() orelse { |
| 18869 | 19004 | const msg = msg: { |
| 18870 | 19005 | const msg = try sema.errMsg(block, inst_src, "expected {}, found {}", .{ |
| 18871 | union_ty, inst_ty, | |
| 19006 | union_ty.fmt(target), inst_ty.fmt(target), | |
| 18872 | 19007 | }); |
| 18873 | 19008 | errdefer msg.destroy(sema.gpa); |
| 18874 | 19009 | try sema.errNote(block, union_ty_src, msg, "cannot coerce enum to untagged union", .{}); |
| ... | ... | @@ -18881,10 +19016,10 @@ fn coerceEnumToUnion( |
| 18881 | 19016 | const enum_tag = try sema.coerce(block, tag_ty, inst, inst_src); |
| 18882 | 19017 | if (try sema.resolveDefinedValue(block, inst_src, enum_tag)) |val| { |
| 18883 | 19018 | const union_obj = union_ty.cast(Type.Payload.Union).?.data; |
| 18884 | const field_index = union_obj.tag_ty.enumTagFieldIndex(val) orelse { | |
| 19019 | const field_index = union_obj.tag_ty.enumTagFieldIndex(val, target) orelse { | |
| 18885 | 19020 | const msg = msg: { |
| 18886 | 19021 | const msg = try sema.errMsg(block, inst_src, "union {} has no tag with value {}", .{ |
| 18887 | union_ty, val.fmtValue(tag_ty), | |
| 19022 | union_ty.fmt(target), val.fmtValue(tag_ty, target), | |
| 18888 | 19023 | }); |
| 18889 | 19024 | errdefer msg.destroy(sema.gpa); |
| 18890 | 19025 | try sema.addDeclaredHereNote(msg, union_ty); |
| ... | ... | @@ -18899,7 +19034,7 @@ fn coerceEnumToUnion( |
| 18899 | 19034 | // also instead of 'union declared here' make it 'field "foo" declared here'. |
| 18900 | 19035 | const msg = msg: { |
| 18901 | 19036 | const msg = try sema.errMsg(block, inst_src, "coercion to union {} must initialize {} field", .{ |
| 18902 | union_ty, field_ty, | |
| 19037 | union_ty.fmt(target), field_ty.fmt(target), | |
| 18903 | 19038 | }); |
| 18904 | 19039 | errdefer msg.destroy(sema.gpa); |
| 18905 | 19040 | try sema.addDeclaredHereNote(msg, union_ty); |
| ... | ... | @@ -18919,7 +19054,7 @@ fn coerceEnumToUnion( |
| 18919 | 19054 | if (tag_ty.isNonexhaustiveEnum()) { |
| 18920 | 19055 | const msg = msg: { |
| 18921 | 19056 | const msg = try sema.errMsg(block, inst_src, "runtime coercion to union {} from non-exhaustive enum", .{ |
| 18922 | union_ty, | |
| 19057 | union_ty.fmt(target), | |
| 18923 | 19058 | }); |
| 18924 | 19059 | errdefer msg.destroy(sema.gpa); |
| 18925 | 19060 | try sema.addDeclaredHereNote(msg, tag_ty); |
| ... | ... | @@ -18937,7 +19072,7 @@ fn coerceEnumToUnion( |
| 18937 | 19072 | // instead of the "union declared here" hint |
| 18938 | 19073 | const msg = msg: { |
| 18939 | 19074 | const msg = try sema.errMsg(block, inst_src, "runtime coercion to union {} which has non-void fields", .{ |
| 18940 | union_ty, | |
| 19075 | union_ty.fmt(target), | |
| 18941 | 19076 | }); |
| 18942 | 19077 | errdefer msg.destroy(sema.gpa); |
| 18943 | 19078 | try sema.addDeclaredHereNote(msg, union_ty); |
| ... | ... | @@ -19020,11 +19155,12 @@ fn coerceArrayLike( |
| 19020 | 19155 | const inst_ty = sema.typeOf(inst); |
| 19021 | 19156 | const inst_len = inst_ty.arrayLen(); |
| 19022 | 19157 | const dest_len = try sema.usizeCast(block, dest_ty_src, dest_ty.arrayLen()); |
| 19158 | const target = sema.mod.getTarget(); | |
| 19023 | 19159 | |
| 19024 | 19160 | if (dest_len != inst_len) { |
| 19025 | 19161 | const msg = msg: { |
| 19026 | 19162 | const msg = try sema.errMsg(block, inst_src, "expected {}, found {}", .{ |
| 19027 | dest_ty, inst_ty, | |
| 19163 | dest_ty.fmt(target), inst_ty.fmt(target), | |
| 19028 | 19164 | }); |
| 19029 | 19165 | errdefer msg.destroy(sema.gpa); |
| 19030 | 19166 | try sema.errNote(block, dest_ty_src, msg, "destination has length {d}", .{dest_len}); |
| ... | ... | @@ -19034,7 +19170,6 @@ fn coerceArrayLike( |
| 19034 | 19170 | return sema.failWithOwnedErrorMsg(block, msg); |
| 19035 | 19171 | } |
| 19036 | 19172 | |
| 19037 | const target = sema.mod.getTarget(); | |
| 19038 | 19173 | const dest_elem_ty = dest_ty.childType(); |
| 19039 | 19174 | const inst_elem_ty = inst_ty.childType(); |
| 19040 | 19175 | const in_memory_result = try sema.coerceInMemoryAllowed(block, dest_elem_ty, inst_elem_ty, false, target, dest_ty_src, inst_src); |
| ... | ... | @@ -19092,11 +19227,12 @@ fn coerceTupleToArray( |
| 19092 | 19227 | const inst_ty = sema.typeOf(inst); |
| 19093 | 19228 | const inst_len = inst_ty.arrayLen(); |
| 19094 | 19229 | const dest_len = try sema.usizeCast(block, dest_ty_src, dest_ty.arrayLen()); |
| 19230 | const target = sema.mod.getTarget(); | |
| 19095 | 19231 | |
| 19096 | 19232 | if (dest_len != inst_len) { |
| 19097 | 19233 | const msg = msg: { |
| 19098 | 19234 | const msg = try sema.errMsg(block, inst_src, "expected {}, found {}", .{ |
| 19099 | dest_ty, inst_ty, | |
| 19235 | dest_ty.fmt(target), inst_ty.fmt(target), | |
| 19100 | 19236 | }); |
| 19101 | 19237 | errdefer msg.destroy(sema.gpa); |
| 19102 | 19238 | try sema.errNote(block, dest_ty_src, msg, "destination has length {d}", .{dest_len}); |
| ... | ... | @@ -19149,7 +19285,8 @@ fn coerceTupleToSlicePtrs( |
| 19149 | 19285 | const tuple_ty = sema.typeOf(ptr_tuple).childType(); |
| 19150 | 19286 | const tuple = try sema.analyzeLoad(block, tuple_src, ptr_tuple, tuple_src); |
| 19151 | 19287 | const slice_info = slice_ty.ptrInfo().data; |
| 19152 | const array_ty = try Type.array(sema.arena, tuple_ty.structFieldCount(), slice_info.sentinel, slice_info.pointee_type); | |
| 19288 | const target = sema.mod.getTarget(); | |
| 19289 | const array_ty = try Type.array(sema.arena, tuple_ty.structFieldCount(), slice_info.sentinel, slice_info.pointee_type, target); | |
| 19153 | 19290 | const array_inst = try sema.coerceTupleToArray(block, array_ty, slice_ty_src, tuple, tuple_src); |
| 19154 | 19291 | if (slice_info.@"align" != 0) { |
| 19155 | 19292 | return sema.fail(block, slice_ty_src, "TODO: override the alignment of the array decl we create here", .{}); |
| ... | ... | @@ -19398,10 +19535,11 @@ fn analyzeLoad( |
| 19398 | 19535 | ptr: Air.Inst.Ref, |
| 19399 | 19536 | ptr_src: LazySrcLoc, |
| 19400 | 19537 | ) CompileError!Air.Inst.Ref { |
| 19538 | const target = sema.mod.getTarget(); | |
| 19401 | 19539 | const ptr_ty = sema.typeOf(ptr); |
| 19402 | 19540 | const elem_ty = switch (ptr_ty.zigTypeTag()) { |
| 19403 | 19541 | .Pointer => ptr_ty.childType(), |
| 19404 | else => return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty}), | |
| 19542 | else => return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ty.fmt(target)}), | |
| 19405 | 19543 | }; |
| 19406 | 19544 | if (try sema.resolveDefinedValue(block, ptr_src, ptr)) |ptr_val| { |
| 19407 | 19545 | if (try sema.pointerDeref(block, ptr_src, ptr_val, ptr_ty)) |elem_val| { |
| ... | ... | @@ -19440,7 +19578,8 @@ fn analyzeSliceLen( |
| 19440 | 19578 | if (slice_val.isUndef()) { |
| 19441 | 19579 | return sema.addConstUndef(Type.usize); |
| 19442 | 19580 | } |
| 19443 | return sema.addIntUnsigned(Type.usize, slice_val.sliceLen()); | |
| 19581 | const target = sema.mod.getTarget(); | |
| 19582 | return sema.addIntUnsigned(Type.usize, slice_val.sliceLen(target)); | |
| 19444 | 19583 | } |
| 19445 | 19584 | try sema.requireRuntimeBlock(block, src); |
| 19446 | 19585 | return block.addTyOp(.slice_len, Type.usize, slice_inst); |
| ... | ... | @@ -19522,9 +19661,10 @@ fn analyzeSlice( |
| 19522 | 19661 | // Slice expressions can operate on a variable whose type is an array. This requires |
| 19523 | 19662 | // the slice operand to be a pointer. In the case of a non-array, it will be a double pointer. |
| 19524 | 19663 | const ptr_ptr_ty = sema.typeOf(ptr_ptr); |
| 19664 | const target = sema.mod.getTarget(); | |
| 19525 | 19665 | const ptr_ptr_child_ty = switch (ptr_ptr_ty.zigTypeTag()) { |
| 19526 | 19666 | .Pointer => ptr_ptr_ty.elemType(), |
| 19527 | else => return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ptr_ty}), | |
| 19667 | else => return sema.fail(block, ptr_src, "expected pointer, found '{}'", .{ptr_ptr_ty.fmt(target)}), | |
| 19528 | 19668 | }; |
| 19529 | 19669 | |
| 19530 | 19670 | var array_ty = ptr_ptr_child_ty; |
| ... | ... | @@ -19564,7 +19704,7 @@ fn analyzeSlice( |
| 19564 | 19704 | elem_ty = ptr_ptr_child_ty.childType(); |
| 19565 | 19705 | }, |
| 19566 | 19706 | }, |
| 19567 | else => return sema.fail(block, ptr_src, "slice of non-array type '{}'", .{ptr_ptr_child_ty}), | |
| 19707 | else => return sema.fail(block, ptr_src, "slice of non-array type '{}'", .{ptr_ptr_child_ty.fmt(target)}), | |
| 19568 | 19708 | } |
| 19569 | 19709 | |
| 19570 | 19710 | const ptr = if (slice_ty.isSlice()) |
| ... | ... | @@ -19587,15 +19727,18 @@ fn analyzeSlice( |
| 19587 | 19727 | if (!end_is_len) { |
| 19588 | 19728 | const end = try sema.coerce(block, Type.usize, uncasted_end_opt, end_src); |
| 19589 | 19729 | if (try sema.resolveMaybeUndefVal(block, end_src, end)) |end_val| { |
| 19590 | if (end_val.compare(.gt, len_val, Type.usize)) { | |
| 19730 | if (end_val.compare(.gt, len_val, Type.usize, target)) { | |
| 19591 | 19731 | return sema.fail( |
| 19592 | 19732 | block, |
| 19593 | 19733 | end_src, |
| 19594 | 19734 | "end index {} out of bounds for array of length {}", |
| 19595 | .{ end_val.fmtValue(Type.usize), len_val.fmtValue(Type.usize) }, | |
| 19735 | .{ | |
| 19736 | end_val.fmtValue(Type.usize, target), | |
| 19737 | len_val.fmtValue(Type.usize, target), | |
| 19738 | }, | |
| 19596 | 19739 | ); |
| 19597 | 19740 | } |
| 19598 | if (end_val.eql(len_val, Type.usize)) { | |
| 19741 | if (end_val.eql(len_val, Type.usize, target)) { | |
| 19599 | 19742 | end_is_len = true; |
| 19600 | 19743 | } |
| 19601 | 19744 | } |
| ... | ... | @@ -19610,18 +19753,21 @@ fn analyzeSlice( |
| 19610 | 19753 | if (try sema.resolveDefinedValue(block, src, ptr_or_slice)) |slice_val| { |
| 19611 | 19754 | var int_payload: Value.Payload.U64 = .{ |
| 19612 | 19755 | .base = .{ .tag = .int_u64 }, |
| 19613 | .data = slice_val.sliceLen(), | |
| 19756 | .data = slice_val.sliceLen(target), | |
| 19614 | 19757 | }; |
| 19615 | 19758 | const slice_len_val = Value.initPayload(&int_payload.base); |
| 19616 | if (end_val.compare(.gt, slice_len_val, Type.usize)) { | |
| 19759 | if (end_val.compare(.gt, slice_len_val, Type.usize, target)) { | |
| 19617 | 19760 | return sema.fail( |
| 19618 | 19761 | block, |
| 19619 | 19762 | end_src, |
| 19620 | 19763 | "end index {} out of bounds for slice of length {}", |
| 19621 | .{ end_val.fmtValue(Type.usize), slice_len_val.fmtValue(Type.usize) }, | |
| 19764 | .{ | |
| 19765 | end_val.fmtValue(Type.usize, target), | |
| 19766 | slice_len_val.fmtValue(Type.usize, target), | |
| 19767 | }, | |
| 19622 | 19768 | ); |
| 19623 | 19769 | } |
| 19624 | if (end_val.eql(slice_len_val, Type.usize)) { | |
| 19770 | if (end_val.eql(slice_len_val, Type.usize, target)) { | |
| 19625 | 19771 | end_is_len = true; |
| 19626 | 19772 | } |
| 19627 | 19773 | } |
| ... | ... | @@ -19654,12 +19800,15 @@ fn analyzeSlice( |
| 19654 | 19800 | // requirement: start <= end |
| 19655 | 19801 | if (try sema.resolveDefinedValue(block, src, end)) |end_val| { |
| 19656 | 19802 | if (try sema.resolveDefinedValue(block, src, start)) |start_val| { |
| 19657 | if (start_val.compare(.gt, end_val, Type.usize)) { | |
| 19803 | if (start_val.compare(.gt, end_val, Type.usize, target)) { | |
| 19658 | 19804 | return sema.fail( |
| 19659 | 19805 | block, |
| 19660 | 19806 | start_src, |
| 19661 | 19807 | "start index {} is larger than end index {}", |
| 19662 | .{ start_val.fmtValue(Type.usize), end_val.fmtValue(Type.usize) }, | |
| 19808 | .{ | |
| 19809 | start_val.fmtValue(Type.usize, target), | |
| 19810 | end_val.fmtValue(Type.usize, target), | |
| 19811 | }, | |
| 19663 | 19812 | ); |
| 19664 | 19813 | } |
| 19665 | 19814 | } |
| ... | ... | @@ -19670,13 +19819,12 @@ fn analyzeSlice( |
| 19670 | 19819 | |
| 19671 | 19820 | const new_ptr_ty_info = sema.typeOf(new_ptr).ptrInfo().data; |
| 19672 | 19821 | const new_allowzero = new_ptr_ty_info.@"allowzero" and sema.typeOf(ptr).ptrSize() != .C; |
| 19673 | const target = sema.mod.getTarget(); | |
| 19674 | 19822 | |
| 19675 | 19823 | if (opt_new_len_val) |new_len_val| { |
| 19676 | const new_len_int = new_len_val.toUnsignedInt(); | |
| 19824 | const new_len_int = new_len_val.toUnsignedInt(target); | |
| 19677 | 19825 | |
| 19678 | 19826 | const return_ty = try Type.ptr(sema.arena, target, .{ |
| 19679 | .pointee_type = try Type.array(sema.arena, new_len_int, sentinel, elem_ty), | |
| 19827 | .pointee_type = try Type.array(sema.arena, new_len_int, sentinel, elem_ty, target), | |
| 19680 | 19828 | .sentinel = null, |
| 19681 | 19829 | .@"align" = new_ptr_ty_info.@"align", |
| 19682 | 19830 | .@"addrspace" = new_ptr_ty_info.@"addrspace", |
| ... | ... | @@ -19746,6 +19894,7 @@ fn cmpNumeric( |
| 19746 | 19894 | |
| 19747 | 19895 | const lhs_ty_tag = lhs_ty.zigTypeTag(); |
| 19748 | 19896 | const rhs_ty_tag = rhs_ty.zigTypeTag(); |
| 19897 | const target = sema.mod.getTarget(); | |
| 19749 | 19898 | |
| 19750 | 19899 | const runtime_src: LazySrcLoc = src: { |
| 19751 | 19900 | if (try sema.resolveMaybeUndefVal(block, lhs_src, lhs)) |lhs_val| { |
| ... | ... | @@ -19760,7 +19909,7 @@ fn cmpNumeric( |
| 19760 | 19909 | return Air.Inst.Ref.bool_false; |
| 19761 | 19910 | } |
| 19762 | 19911 | } |
| 19763 | if (Value.compareHetero(lhs_val, op, rhs_val)) { | |
| 19912 | if (try Value.compareHeteroAdvanced(lhs_val, op, rhs_val, target, sema.kit(block, src))) { | |
| 19764 | 19913 | return Air.Inst.Ref.bool_true; |
| 19765 | 19914 | } else { |
| 19766 | 19915 | return Air.Inst.Ref.bool_false; |
| ... | ... | @@ -19789,7 +19938,6 @@ fn cmpNumeric( |
| 19789 | 19938 | .Float, .ComptimeFloat => true, |
| 19790 | 19939 | else => false, |
| 19791 | 19940 | }; |
| 19792 | const target = sema.mod.getTarget(); | |
| 19793 | 19941 | if (lhs_is_float and rhs_is_float) { |
| 19794 | 19942 | // Implicit cast the smaller one to the larger one. |
| 19795 | 19943 | const dest_ty = x: { |
| ... | ... | @@ -19846,7 +19994,7 @@ fn cmpNumeric( |
| 19846 | 19994 | } |
| 19847 | 19995 | if (lhs_is_float) { |
| 19848 | 19996 | var bigint_space: Value.BigIntSpace = undefined; |
| 19849 | var bigint = try lhs_val.toBigInt(&bigint_space).toManaged(sema.gpa); | |
| 19997 | var bigint = try lhs_val.toBigInt(&bigint_space, target).toManaged(sema.gpa); | |
| 19850 | 19998 | defer bigint.deinit(); |
| 19851 | 19999 | if (lhs_val.floatHasFraction()) { |
| 19852 | 20000 | switch (op) { |
| ... | ... | @@ -19892,7 +20040,7 @@ fn cmpNumeric( |
| 19892 | 20040 | } |
| 19893 | 20041 | if (rhs_is_float) { |
| 19894 | 20042 | var bigint_space: Value.BigIntSpace = undefined; |
| 19895 | var bigint = try rhs_val.toBigInt(&bigint_space).toManaged(sema.gpa); | |
| 20043 | var bigint = try rhs_val.toBigInt(&bigint_space, target).toManaged(sema.gpa); | |
| 19896 | 20044 | defer bigint.deinit(); |
| 19897 | 20045 | if (rhs_val.floatHasFraction()) { |
| 19898 | 20046 | switch (op) { |
| ... | ... | @@ -19950,6 +20098,7 @@ fn cmpVector( |
| 19950 | 20098 | try sema.checkVectorizableBinaryOperands(block, src, lhs_ty, rhs_ty, lhs_src, rhs_src); |
| 19951 | 20099 | |
| 19952 | 20100 | const result_ty = try Type.vector(sema.arena, lhs_ty.vectorLen(), Type.@"bool"); |
| 20101 | const target = sema.mod.getTarget(); | |
| 19953 | 20102 | |
| 19954 | 20103 | const runtime_src: LazySrcLoc = src: { |
| 19955 | 20104 | if (try sema.resolveMaybeUndefVal(block, lhs_src, lhs)) |lhs_val| { |
| ... | ... | @@ -19957,7 +20106,7 @@ fn cmpVector( |
| 19957 | 20106 | if (lhs_val.isUndef() or rhs_val.isUndef()) { |
| 19958 | 20107 | return sema.addConstUndef(result_ty); |
| 19959 | 20108 | } |
| 19960 | const cmp_val = try lhs_val.compareVector(op, rhs_val, lhs_ty, sema.arena); | |
| 20109 | const cmp_val = try lhs_val.compareVector(op, rhs_val, lhs_ty, sema.arena, target); | |
| 19961 | 20110 | return sema.addConstant(result_ty, cmp_val); |
| 19962 | 20111 | } else { |
| 19963 | 20112 | break :src rhs_src; |
| ... | ... | @@ -20108,7 +20257,7 @@ fn resolvePeerTypes( |
| 20108 | 20257 | const candidate_ty_tag = try candidate_ty.zigTypeTagOrPoison(); |
| 20109 | 20258 | const chosen_ty_tag = try chosen_ty.zigTypeTagOrPoison(); |
| 20110 | 20259 | |
| 20111 | if (candidate_ty.eql(chosen_ty)) | |
| 20260 | if (candidate_ty.eql(chosen_ty, target)) | |
| 20112 | 20261 | continue; |
| 20113 | 20262 | |
| 20114 | 20263 | switch (candidate_ty_tag) { |
| ... | ... | @@ -20522,14 +20671,17 @@ fn resolvePeerTypes( |
| 20522 | 20671 | ); |
| 20523 | 20672 | |
| 20524 | 20673 | const msg = msg: { |
| 20525 | const msg = try sema.errMsg(block, src, "incompatible types: '{}' and '{}'", .{ chosen_ty, candidate_ty }); | |
| 20674 | const msg = try sema.errMsg(block, src, "incompatible types: '{}' and '{}'", .{ | |
| 20675 | chosen_ty.fmt(target), | |
| 20676 | candidate_ty.fmt(target), | |
| 20677 | }); | |
| 20526 | 20678 | errdefer msg.destroy(sema.gpa); |
| 20527 | 20679 | |
| 20528 | 20680 | if (chosen_src) |src_loc| |
| 20529 | try sema.errNote(block, src_loc, msg, "type '{}' here", .{chosen_ty}); | |
| 20681 | try sema.errNote(block, src_loc, msg, "type '{}' here", .{chosen_ty.fmt(target)}); | |
| 20530 | 20682 | |
| 20531 | 20683 | if (candidate_src) |src_loc| |
| 20532 | try sema.errNote(block, src_loc, msg, "type '{}' here", .{candidate_ty}); | |
| 20684 | try sema.errNote(block, src_loc, msg, "type '{}' here", .{candidate_ty.fmt(target)}); | |
| 20533 | 20685 | |
| 20534 | 20686 | break :msg msg; |
| 20535 | 20687 | }; |
| ... | ... | @@ -20557,7 +20709,7 @@ fn resolvePeerTypes( |
| 20557 | 20709 | else |
| 20558 | 20710 | new_ptr_ty; |
| 20559 | 20711 | const set_ty = err_set_ty orelse return opt_ptr_ty; |
| 20560 | return try Module.errorUnionType(sema.arena, set_ty, opt_ptr_ty); | |
| 20712 | return try Type.errorUnion(sema.arena, set_ty, opt_ptr_ty, target); | |
| 20561 | 20713 | } |
| 20562 | 20714 | |
| 20563 | 20715 | if (seen_const) { |
| ... | ... | @@ -20573,7 +20725,7 @@ fn resolvePeerTypes( |
| 20573 | 20725 | else |
| 20574 | 20726 | new_ptr_ty; |
| 20575 | 20727 | const set_ty = err_set_ty orelse chosen_ty.errorUnionSet(); |
| 20576 | return try Module.errorUnionType(sema.arena, set_ty, opt_ptr_ty); | |
| 20728 | return try Type.errorUnion(sema.arena, set_ty, opt_ptr_ty, target); | |
| 20577 | 20729 | }, |
| 20578 | 20730 | .Pointer => { |
| 20579 | 20731 | var info = chosen_ty.ptrInfo(); |
| ... | ... | @@ -20584,7 +20736,7 @@ fn resolvePeerTypes( |
| 20584 | 20736 | else |
| 20585 | 20737 | new_ptr_ty; |
| 20586 | 20738 | const set_ty = err_set_ty orelse return opt_ptr_ty; |
| 20587 | return try Module.errorUnionType(sema.arena, set_ty, opt_ptr_ty); | |
| 20739 | return try Type.errorUnion(sema.arena, set_ty, opt_ptr_ty, target); | |
| 20588 | 20740 | }, |
| 20589 | 20741 | else => return chosen_ty, |
| 20590 | 20742 | } |
| ... | ... | @@ -20596,16 +20748,16 @@ fn resolvePeerTypes( |
| 20596 | 20748 | else => try Type.optional(sema.arena, chosen_ty), |
| 20597 | 20749 | }; |
| 20598 | 20750 | const set_ty = err_set_ty orelse return opt_ty; |
| 20599 | return try Module.errorUnionType(sema.arena, set_ty, opt_ty); | |
| 20751 | return try Type.errorUnion(sema.arena, set_ty, opt_ty, target); | |
| 20600 | 20752 | } |
| 20601 | 20753 | |
| 20602 | 20754 | if (err_set_ty) |ty| switch (chosen_ty.zigTypeTag()) { |
| 20603 | 20755 | .ErrorSet => return ty, |
| 20604 | 20756 | .ErrorUnion => { |
| 20605 | 20757 | const payload_ty = chosen_ty.errorUnionPayload(); |
| 20606 | return try Module.errorUnionType(sema.arena, ty, payload_ty); | |
| 20758 | return try Type.errorUnion(sema.arena, ty, payload_ty, target); | |
| 20607 | 20759 | }, |
| 20608 | else => return try Module.errorUnionType(sema.arena, ty, chosen_ty), | |
| 20760 | else => return try Type.errorUnion(sema.arena, ty, chosen_ty, target), | |
| 20609 | 20761 | }; |
| 20610 | 20762 | |
| 20611 | 20763 | return chosen_ty; |
| ... | ... | @@ -20624,7 +20776,7 @@ pub fn resolveFnTypes( |
| 20624 | 20776 | } |
| 20625 | 20777 | } |
| 20626 | 20778 | |
| 20627 | fn resolveTypeLayout( | |
| 20779 | pub fn resolveTypeLayout( | |
| 20628 | 20780 | sema: *Sema, |
| 20629 | 20781 | block: *Block, |
| 20630 | 20782 | src: LazySrcLoc, |
| ... | ... | @@ -20662,11 +20814,12 @@ fn resolveStructLayout( |
| 20662 | 20814 | ) CompileError!void { |
| 20663 | 20815 | const resolved_ty = try sema.resolveTypeFields(block, src, ty); |
| 20664 | 20816 | if (resolved_ty.castTag(.@"struct")) |payload| { |
| 20817 | const target = sema.mod.getTarget(); | |
| 20665 | 20818 | const struct_obj = payload.data; |
| 20666 | 20819 | switch (struct_obj.status) { |
| 20667 | 20820 | .none, .have_field_types => {}, |
| 20668 | 20821 | .field_types_wip, .layout_wip => { |
| 20669 | return sema.fail(block, src, "struct {} depends on itself", .{ty}); | |
| 20822 | return sema.fail(block, src, "struct {} depends on itself", .{ty.fmt(target)}); | |
| 20670 | 20823 | }, |
| 20671 | 20824 | .have_layout, .fully_resolved_wip, .fully_resolved => return, |
| 20672 | 20825 | } |
| ... | ... | @@ -20694,10 +20847,11 @@ fn resolveUnionLayout( |
| 20694 | 20847 | ) CompileError!void { |
| 20695 | 20848 | const resolved_ty = try sema.resolveTypeFields(block, src, ty); |
| 20696 | 20849 | const union_obj = resolved_ty.cast(Type.Payload.Union).?.data; |
| 20850 | const target = sema.mod.getTarget(); | |
| 20697 | 20851 | switch (union_obj.status) { |
| 20698 | 20852 | .none, .have_field_types => {}, |
| 20699 | 20853 | .field_types_wip, .layout_wip => { |
| 20700 | return sema.fail(block, src, "union {} depends on itself", .{ty}); | |
| 20854 | return sema.fail(block, src, "union {} depends on itself", .{ty.fmt(target)}); | |
| 20701 | 20855 | }, |
| 20702 | 20856 | .have_layout, .fully_resolved_wip, .fully_resolved => return, |
| 20703 | 20857 | } |
| ... | ... | @@ -20793,7 +20947,7 @@ fn resolveUnionFully( |
| 20793 | 20947 | union_obj.status = .fully_resolved; |
| 20794 | 20948 | } |
| 20795 | 20949 | |
| 20796 | fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!Type { | |
| 20950 | pub fn resolveTypeFields(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!Type { | |
| 20797 | 20951 | switch (ty.tag()) { |
| 20798 | 20952 | .@"struct" => { |
| 20799 | 20953 | const struct_obj = ty.castTag(.@"struct").?.data; |
| ... | ... | @@ -20828,10 +20982,11 @@ fn resolveTypeFieldsStruct( |
| 20828 | 20982 | ty: Type, |
| 20829 | 20983 | struct_obj: *Module.Struct, |
| 20830 | 20984 | ) CompileError!void { |
| 20985 | const target = sema.mod.getTarget(); | |
| 20831 | 20986 | switch (struct_obj.status) { |
| 20832 | 20987 | .none => {}, |
| 20833 | 20988 | .field_types_wip => { |
| 20834 | return sema.fail(block, src, "struct {} depends on itself", .{ty}); | |
| 20989 | return sema.fail(block, src, "struct {} depends on itself", .{ty.fmt(target)}); | |
| 20835 | 20990 | }, |
| 20836 | 20991 | .have_field_types, |
| 20837 | 20992 | .have_layout, |
| ... | ... | @@ -20858,10 +21013,11 @@ fn resolveTypeFieldsUnion( |
| 20858 | 21013 | ty: Type, |
| 20859 | 21014 | union_obj: *Module.Union, |
| 20860 | 21015 | ) CompileError!void { |
| 21016 | const target = sema.mod.getTarget(); | |
| 20861 | 21017 | switch (union_obj.status) { |
| 20862 | 21018 | .none => {}, |
| 20863 | 21019 | .field_types_wip => { |
| 20864 | return sema.fail(block, src, "union {} depends on itself", .{ty}); | |
| 21020 | return sema.fail(block, src, "union {} depends on itself", .{ty.fmt(target)}); | |
| 20865 | 21021 | }, |
| 20866 | 21022 | .have_field_types, |
| 20867 | 21023 | .have_layout, |
| ... | ... | @@ -21218,6 +21374,8 @@ fn semaUnionFields(mod: *Module, union_obj: *Module.Union) CompileError!void { |
| 21218 | 21374 | enum_field_names = &union_obj.tag_ty.castTag(.enum_simple).?.data.fields; |
| 21219 | 21375 | } |
| 21220 | 21376 | |
| 21377 | const target = sema.mod.getTarget(); | |
| 21378 | ||
| 21221 | 21379 | const bits_per_field = 4; |
| 21222 | 21380 | const fields_per_u32 = 32 / bits_per_field; |
| 21223 | 21381 | const bit_bags_count = std.math.divCeil(usize, fields_len, fields_per_u32) catch unreachable; |
| ... | ... | @@ -21275,16 +21433,22 @@ fn semaUnionFields(mod: *Module, union_obj: *Module.Union) CompileError!void { |
| 21275 | 21433 | // This puts the memory into the union arena, not the enum arena, but |
| 21276 | 21434 | // it is OK since they share the same lifetime. |
| 21277 | 21435 | const copied_val = try val.copy(decl_arena_allocator); |
| 21278 | map.putAssumeCapacityContext(copied_val, {}, .{ .ty = int_tag_ty }); | |
| 21436 | map.putAssumeCapacityContext(copied_val, {}, .{ | |
| 21437 | .ty = int_tag_ty, | |
| 21438 | .target = target, | |
| 21439 | }); | |
| 21279 | 21440 | } else { |
| 21280 | 21441 | const val = if (last_tag_val) |val| |
| 21281 | try val.intAdd(Value.one, int_tag_ty, sema.arena) | |
| 21442 | try val.intAdd(Value.one, int_tag_ty, sema.arena, target) | |
| 21282 | 21443 | else |
| 21283 | 21444 | Value.zero; |
| 21284 | 21445 | last_tag_val = val; |
| 21285 | 21446 | |
| 21286 | 21447 | const copied_val = try val.copy(decl_arena_allocator); |
| 21287 | map.putAssumeCapacityContext(copied_val, {}, .{ .ty = int_tag_ty }); | |
| 21448 | map.putAssumeCapacityContext(copied_val, {}, .{ | |
| 21449 | .ty = int_tag_ty, | |
| 21450 | .target = target, | |
| 21451 | }); | |
| 21288 | 21452 | } |
| 21289 | 21453 | } |
| 21290 | 21454 | |
| ... | ... | @@ -21359,7 +21523,10 @@ fn generateUnionTagTypeNumbered( |
| 21359 | 21523 | }; |
| 21360 | 21524 | // Here we pre-allocate the maps using the decl arena. |
| 21361 | 21525 | try enum_obj.fields.ensureTotalCapacity(new_decl_arena_allocator, fields_len); |
| 21362 | try enum_obj.values.ensureTotalCapacityContext(new_decl_arena_allocator, fields_len, .{ .ty = int_ty }); | |
| 21526 | try enum_obj.values.ensureTotalCapacityContext(new_decl_arena_allocator, fields_len, .{ | |
| 21527 | .ty = int_ty, | |
| 21528 | .target = sema.mod.getTarget(), | |
| 21529 | }); | |
| 21363 | 21530 | try new_decl.finalizeNewArena(&new_decl_arena); |
| 21364 | 21531 | return enum_ty; |
| 21365 | 21532 | } |
| ... | ... | @@ -21962,7 +22129,7 @@ fn pointerDeref(sema: *Sema, block: *Block, src: LazySrcLoc, ptr_val: Value, ptr |
| 21962 | 22129 | // The type is not in-memory coercible or the direct dereference failed, so it must |
| 21963 | 22130 | // be bitcast according to the pointer type we are performing the load through. |
| 21964 | 22131 | if (!load_ty.hasWellDefinedLayout()) |
| 21965 | return sema.fail(block, src, "comptime dereference requires {} to have a well-defined layout, but it does not.", .{load_ty}); | |
| 22132 | return sema.fail(block, src, "comptime dereference requires {} to have a well-defined layout, but it does not.", .{load_ty.fmt(target)}); | |
| 21966 | 22133 | |
| 21967 | 22134 | const load_sz = try sema.typeAbiSize(block, src, load_ty); |
| 21968 | 22135 | |
| ... | ... | @@ -21977,11 +22144,11 @@ fn pointerDeref(sema: *Sema, block: *Block, src: LazySrcLoc, ptr_val: Value, ptr |
| 21977 | 22144 | if (deref.ty_without_well_defined_layout) |bad_ty| { |
| 21978 | 22145 | // We got no parent for bit-casting, or the parent we got was too small. Either way, the problem |
| 21979 | 22146 | // is that some type we encountered when de-referencing does not have a well-defined layout. |
| 21980 | return sema.fail(block, src, "comptime dereference requires {} to have a well-defined layout, but it does not.", .{bad_ty}); | |
| 22147 | return sema.fail(block, src, "comptime dereference requires {} to have a well-defined layout, but it does not.", .{bad_ty.fmt(target)}); | |
| 21981 | 22148 | } else { |
| 21982 | 22149 | // If all encountered types had well-defined layouts, the parent is the root decl and it just |
| 21983 | 22150 | // wasn't big enough for the load. |
| 21984 | return sema.fail(block, src, "dereference of {} exceeds bounds of containing decl of type {}", .{ ptr_ty, deref.parent.?.tv.ty }); | |
| 22151 | return sema.fail(block, src, "dereference of {} exceeds bounds of containing decl of type {}", .{ ptr_ty.fmt(target), deref.parent.?.tv.ty.fmt(target) }); | |
| 21985 | 22152 | } |
| 21986 | 22153 | } |
| 21987 | 22154 | |
| ... | ... | @@ -22060,7 +22227,9 @@ fn typePtrOrOptionalPtrTy( |
| 22060 | 22227 | /// This function returns false negatives when structs and unions are having their |
| 22061 | 22228 | /// field types resolved. |
| 22062 | 22229 | /// TODO assert the return value matches `ty.comptimeOnly` |
| 22063 | fn typeRequiresComptime(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!bool { | |
| 22230 | /// TODO merge these implementations together with the "advanced"/sema_kit pattern seen | |
| 22231 | /// elsewhere in value.zig | |
| 22232 | pub fn typeRequiresComptime(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) CompileError!bool { | |
| 22064 | 22233 | return switch (ty.tag()) { |
| 22065 | 22234 | .u1, |
| 22066 | 22235 | .u8, |
| ... | ... | @@ -22266,6 +22435,7 @@ fn typeAbiSize(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !u64 { |
| 22266 | 22435 | return ty.abiSize(target); |
| 22267 | 22436 | } |
| 22268 | 22437 | |
| 22438 | /// TODO merge with Type.abiAlignmentAdvanced | |
| 22269 | 22439 | fn typeAbiAlignment(sema: *Sema, block: *Block, src: LazySrcLoc, ty: Type) !u32 { |
| 22270 | 22440 | try sema.resolveTypeLayout(block, src, ty); |
| 22271 | 22441 | const target = sema.mod.getTarget(); |
| ... | ... | @@ -22344,7 +22514,12 @@ fn anonStructFieldIndex( |
| 22344 | 22514 | return @intCast(u32, i); |
| 22345 | 22515 | } |
| 22346 | 22516 | } |
| 22517 | const target = sema.mod.getTarget(); | |
| 22347 | 22518 | return sema.fail(block, field_src, "anonymous struct {} has no such field '{s}'", .{ |
| 22348 | struct_ty, field_name, | |
| 22519 | struct_ty.fmt(target), field_name, | |
| 22349 | 22520 | }); |
| 22350 | 22521 | } |
| 22522 | ||
| 22523 | fn kit(sema: *Sema, block: *Block, src: LazySrcLoc) Module.WipAnalysis { | |
| 22524 | return .{ .sema = sema, .block = block, .src = src }; | |
| 22525 | } |
src/TypedValue.zig+35-22| ... | ... | @@ -3,6 +3,7 @@ const Type = @import("type.zig").Type; |
| 3 | 3 | const Value = @import("value.zig").Value; |
| 4 | 4 | const Allocator = std.mem.Allocator; |
| 5 | 5 | const TypedValue = @This(); |
| 6 | const Target = std.Target; | |
| 6 | 7 | |
| 7 | 8 | ty: Type, |
| 8 | 9 | val: Value, |
| ... | ... | @@ -30,13 +31,13 @@ pub fn copy(self: TypedValue, arena: Allocator) error{OutOfMemory}!TypedValue { |
| 30 | 31 | }; |
| 31 | 32 | } |
| 32 | 33 | |
| 33 | pub fn eql(a: TypedValue, b: TypedValue) bool { | |
| 34 | if (!a.ty.eql(b.ty)) return false; | |
| 35 | return a.val.eql(b.val, a.ty); | |
| 34 | pub fn eql(a: TypedValue, b: TypedValue, target: std.Target) bool { | |
| 35 | if (!a.ty.eql(b.ty, target)) return false; | |
| 36 | return a.val.eql(b.val, a.ty, target); | |
| 36 | 37 | } |
| 37 | 38 | |
| 38 | pub fn hash(tv: TypedValue, hasher: *std.hash.Wyhash) void { | |
| 39 | return tv.val.hash(tv.ty, hasher); | |
| 39 | pub fn hash(tv: TypedValue, hasher: *std.hash.Wyhash, target: std.Target) void { | |
| 40 | return tv.val.hash(tv.ty, hasher, target); | |
| 40 | 41 | } |
| 41 | 42 | |
| 42 | 43 | pub fn enumToInt(tv: TypedValue, buffer: *Value.Payload.U64) Value { |
| ... | ... | @@ -45,21 +46,28 @@ pub fn enumToInt(tv: TypedValue, buffer: *Value.Payload.U64) Value { |
| 45 | 46 | |
| 46 | 47 | const max_aggregate_items = 100; |
| 47 | 48 | |
| 48 | pub fn format( | |
| 49 | const FormatContext = struct { | |
| 49 | 50 | tv: TypedValue, |
| 51 | target: Target, | |
| 52 | }; | |
| 53 | ||
| 54 | pub fn format( | |
| 55 | ctx: FormatContext, | |
| 50 | 56 | comptime fmt: []const u8, |
| 51 | 57 | options: std.fmt.FormatOptions, |
| 52 | 58 | writer: anytype, |
| 53 | 59 | ) !void { |
| 60 | _ = options; | |
| 54 | 61 | comptime std.debug.assert(fmt.len == 0); |
| 55 | return tv.print(options, writer, 3); | |
| 62 | return ctx.tv.print(writer, 3, ctx.target); | |
| 56 | 63 | } |
| 57 | 64 | |
| 65 | /// Prints the Value according to the Type, not according to the Value Tag. | |
| 58 | 66 | pub fn print( |
| 59 | 67 | tv: TypedValue, |
| 60 | options: std.fmt.FormatOptions, | |
| 61 | 68 | writer: anytype, |
| 62 | 69 | level: u8, |
| 70 | target: std.Target, | |
| 63 | 71 | ) @TypeOf(writer).Error!void { |
| 64 | 72 | var val = tv.val; |
| 65 | 73 | var ty = tv.ty; |
| ... | ... | @@ -148,7 +156,7 @@ pub fn print( |
| 148 | 156 | try print(.{ |
| 149 | 157 | .ty = fields[i].ty, |
| 150 | 158 | .val = vals[i], |
| 151 | }, options, writer, level - 1); | |
| 159 | }, writer, level - 1, target); | |
| 152 | 160 | } |
| 153 | 161 | return writer.writeAll(" }"); |
| 154 | 162 | } else { |
| ... | ... | @@ -162,7 +170,7 @@ pub fn print( |
| 162 | 170 | try print(.{ |
| 163 | 171 | .ty = elem_ty, |
| 164 | 172 | .val = vals[i], |
| 165 | }, options, writer, level - 1); | |
| 173 | }, writer, level - 1, target); | |
| 166 | 174 | } |
| 167 | 175 | return writer.writeAll(" }"); |
| 168 | 176 | } |
| ... | ... | @@ -177,12 +185,12 @@ pub fn print( |
| 177 | 185 | try print(.{ |
| 178 | 186 | .ty = ty.unionTagType().?, |
| 179 | 187 | .val = union_val.tag, |
| 180 | }, options, writer, level - 1); | |
| 188 | }, writer, level - 1, target); | |
| 181 | 189 | try writer.writeAll(" = "); |
| 182 | 190 | try print(.{ |
| 183 | .ty = ty.unionFieldType(union_val.tag), | |
| 191 | .ty = ty.unionFieldType(union_val.tag, target), | |
| 184 | 192 | .val = union_val.val, |
| 185 | }, options, writer, level - 1); | |
| 193 | }, writer, level - 1, target); | |
| 186 | 194 | |
| 187 | 195 | return writer.writeAll(" }"); |
| 188 | 196 | }, |
| ... | ... | @@ -197,7 +205,7 @@ pub fn print( |
| 197 | 205 | }, |
| 198 | 206 | .bool_true => return writer.writeAll("true"), |
| 199 | 207 | .bool_false => return writer.writeAll("false"), |
| 200 | .ty => return val.castTag(.ty).?.data.format("", options, writer), | |
| 208 | .ty => return val.castTag(.ty).?.data.print(writer, target), | |
| 201 | 209 | .int_type => { |
| 202 | 210 | const int_type = val.castTag(.int_type).?.data; |
| 203 | 211 | return writer.print("{s}{d}", .{ |
| ... | ... | @@ -205,10 +213,15 @@ pub fn print( |
| 205 | 213 | int_type.bits, |
| 206 | 214 | }); |
| 207 | 215 | }, |
| 208 | .int_u64 => return std.fmt.formatIntValue(val.castTag(.int_u64).?.data, "", options, writer), | |
| 209 | .int_i64 => return std.fmt.formatIntValue(val.castTag(.int_i64).?.data, "", options, writer), | |
| 216 | .int_u64 => return std.fmt.formatIntValue(val.castTag(.int_u64).?.data, "", .{}, writer), | |
| 217 | .int_i64 => return std.fmt.formatIntValue(val.castTag(.int_i64).?.data, "", .{}, writer), | |
| 210 | 218 | .int_big_positive => return writer.print("{}", .{val.castTag(.int_big_positive).?.asBigInt()}), |
| 211 | 219 | .int_big_negative => return writer.print("{}", .{val.castTag(.int_big_negative).?.asBigInt()}), |
| 220 | .lazy_align => { | |
| 221 | const sub_ty = val.castTag(.lazy_align).?.data; | |
| 222 | const x = sub_ty.abiAlignment(target); | |
| 223 | return writer.print("{d}", .{x}); | |
| 224 | }, | |
| 212 | 225 | .function => return writer.print("(function '{s}')", .{val.castTag(.function).?.data.owner_decl.name}), |
| 213 | 226 | .extern_fn => return writer.writeAll("(extern function)"), |
| 214 | 227 | .variable => return writer.writeAll("(variable)"), |
| ... | ... | @@ -220,7 +233,7 @@ pub fn print( |
| 220 | 233 | return print(.{ |
| 221 | 234 | .ty = decl.ty, |
| 222 | 235 | .val = decl.val, |
| 223 | }, options, writer, level - 1); | |
| 236 | }, writer, level - 1, target); | |
| 224 | 237 | }, |
| 225 | 238 | .decl_ref => { |
| 226 | 239 | const decl = val.castTag(.decl_ref).?.data; |
| ... | ... | @@ -230,7 +243,7 @@ pub fn print( |
| 230 | 243 | return print(.{ |
| 231 | 244 | .ty = decl.ty, |
| 232 | 245 | .val = decl.val, |
| 233 | }, options, writer, level - 1); | |
| 246 | }, writer, level - 1, target); | |
| 234 | 247 | }, |
| 235 | 248 | .elem_ptr => { |
| 236 | 249 | const elem_ptr = val.castTag(.elem_ptr).?.data; |
| ... | ... | @@ -238,7 +251,7 @@ pub fn print( |
| 238 | 251 | try print(.{ |
| 239 | 252 | .ty = elem_ptr.elem_ty, |
| 240 | 253 | .val = elem_ptr.array_ptr, |
| 241 | }, options, writer, level - 1); | |
| 254 | }, writer, level - 1, target); | |
| 242 | 255 | return writer.print("[{}]", .{elem_ptr.index}); |
| 243 | 256 | }, |
| 244 | 257 | .field_ptr => { |
| ... | ... | @@ -247,7 +260,7 @@ pub fn print( |
| 247 | 260 | try print(.{ |
| 248 | 261 | .ty = field_ptr.container_ty, |
| 249 | 262 | .val = field_ptr.container_ptr, |
| 250 | }, options, writer, level - 1); | |
| 263 | }, writer, level - 1, target); | |
| 251 | 264 | |
| 252 | 265 | if (field_ptr.container_ty.zigTypeTag() == .Struct) { |
| 253 | 266 | const field_name = field_ptr.container_ty.structFields().keys()[field_ptr.field_index]; |
| ... | ... | @@ -275,7 +288,7 @@ pub fn print( |
| 275 | 288 | }; |
| 276 | 289 | while (i < max_aggregate_items) : (i += 1) { |
| 277 | 290 | if (i != 0) try writer.writeAll(", "); |
| 278 | try print(elem_tv, options, writer, level - 1); | |
| 291 | try print(elem_tv, writer, level - 1, target); | |
| 279 | 292 | } |
| 280 | 293 | return writer.writeAll(" }"); |
| 281 | 294 | }, |
| ... | ... | @@ -287,7 +300,7 @@ pub fn print( |
| 287 | 300 | try print(.{ |
| 288 | 301 | .ty = ty.elemType2(), |
| 289 | 302 | .val = ty.sentinel().?, |
| 290 | }, options, writer, level - 1); | |
| 303 | }, writer, level - 1, target); | |
| 291 | 304 | return writer.writeAll(" }"); |
| 292 | 305 | }, |
| 293 | 306 | .slice => return writer.writeAll("(slice)"), |
src/arch/aarch64/CodeGen.zig+20-13| ... | ... | @@ -796,7 +796,9 @@ fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void { |
| 796 | 796 | const index = dbg_out.dbg_info.items.len; |
| 797 | 797 | try dbg_out.dbg_info.resize(index + 4); // DW.AT.type, DW.FORM.ref4 |
| 798 | 798 | |
| 799 | const gop = try dbg_out.dbg_info_type_relocs.getOrPut(self.gpa, ty); | |
| 799 | const gop = try dbg_out.dbg_info_type_relocs.getOrPutContext(self.gpa, ty, .{ | |
| 800 | .target = self.target.*, | |
| 801 | }); | |
| 800 | 802 | if (!gop.found_existing) { |
| 801 | 803 | gop.value_ptr.* = .{ |
| 802 | 804 | .off = undefined, |
| ... | ... | @@ -835,8 +837,9 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 835 | 837 | return self.next_stack_offset; |
| 836 | 838 | } |
| 837 | 839 | |
| 840 | const target = self.target.*; | |
| 838 | 841 | const abi_size = math.cast(u32, elem_ty.abiSize(self.target.*)) catch { |
| 839 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty}); | |
| 842 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(target)}); | |
| 840 | 843 | }; |
| 841 | 844 | // TODO swap this for inst.ty.ptrAlign |
| 842 | 845 | const abi_align = elem_ty.abiAlignment(self.target.*); |
| ... | ... | @@ -845,8 +848,9 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 845 | 848 | |
| 846 | 849 | fn allocRegOrMem(self: *Self, inst: Air.Inst.Index, reg_ok: bool) !MCValue { |
| 847 | 850 | const elem_ty = self.air.typeOfIndex(inst); |
| 851 | const target = self.target.*; | |
| 848 | 852 | const abi_size = math.cast(u32, elem_ty.abiSize(self.target.*)) catch { |
| 849 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty}); | |
| 853 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(target)}); | |
| 850 | 854 | }; |
| 851 | 855 | const abi_align = elem_ty.abiAlignment(self.target.*); |
| 852 | 856 | if (abi_align > self.stack_align) |
| ... | ... | @@ -1372,6 +1376,7 @@ fn binOp( |
| 1372 | 1376 | lhs_ty: Type, |
| 1373 | 1377 | rhs_ty: Type, |
| 1374 | 1378 | ) InnerError!MCValue { |
| 1379 | const target = self.target.*; | |
| 1375 | 1380 | switch (tag) { |
| 1376 | 1381 | // Arithmetic operations on integers and floats |
| 1377 | 1382 | .add, |
| ... | ... | @@ -1381,7 +1386,7 @@ fn binOp( |
| 1381 | 1386 | .Float => return self.fail("TODO binary operations on floats", .{}), |
| 1382 | 1387 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 1383 | 1388 | .Int => { |
| 1384 | assert(lhs_ty.eql(rhs_ty)); | |
| 1389 | assert(lhs_ty.eql(rhs_ty, target)); | |
| 1385 | 1390 | const int_info = lhs_ty.intInfo(self.target.*); |
| 1386 | 1391 | if (int_info.bits <= 64) { |
| 1387 | 1392 | // Only say yes if the operation is |
| ... | ... | @@ -1418,7 +1423,7 @@ fn binOp( |
| 1418 | 1423 | switch (lhs_ty.zigTypeTag()) { |
| 1419 | 1424 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 1420 | 1425 | .Int => { |
| 1421 | assert(lhs_ty.eql(rhs_ty)); | |
| 1426 | assert(lhs_ty.eql(rhs_ty, target)); | |
| 1422 | 1427 | const int_info = lhs_ty.intInfo(self.target.*); |
| 1423 | 1428 | if (int_info.bits <= 64) { |
| 1424 | 1429 | // TODO add optimisations for multiplication |
| ... | ... | @@ -1440,7 +1445,7 @@ fn binOp( |
| 1440 | 1445 | switch (lhs_ty.zigTypeTag()) { |
| 1441 | 1446 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 1442 | 1447 | .Int => { |
| 1443 | assert(lhs_ty.eql(rhs_ty)); | |
| 1448 | assert(lhs_ty.eql(rhs_ty, target)); | |
| 1444 | 1449 | const int_info = lhs_ty.intInfo(self.target.*); |
| 1445 | 1450 | if (int_info.bits <= 64) { |
| 1446 | 1451 | // TODO implement bitwise operations with immediates |
| ... | ... | @@ -2348,11 +2353,12 @@ fn airArg(self: *Self, inst: Air.Inst.Index) !void { |
| 2348 | 2353 | const ty = self.air.typeOfIndex(inst); |
| 2349 | 2354 | |
| 2350 | 2355 | const result = self.args[arg_index]; |
| 2356 | const target = self.target.*; | |
| 2351 | 2357 | const mcv = switch (result) { |
| 2352 | 2358 | // Copy registers to the stack |
| 2353 | 2359 | .register => |reg| blk: { |
| 2354 | 2360 | const abi_size = math.cast(u32, ty.abiSize(self.target.*)) catch { |
| 2355 | return self.fail("type '{}' too big to fit into stack frame", .{ty}); | |
| 2361 | return self.fail("type '{}' too big to fit into stack frame", .{ty.fmt(target)}); | |
| 2356 | 2362 | }; |
| 2357 | 2363 | const abi_align = ty.abiAlignment(self.target.*); |
| 2358 | 2364 | const stack_offset = try self.allocMem(inst, abi_size, abi_align); |
| ... | ... | @@ -3879,7 +3885,7 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl: *Module.Decl) InnerError!MCVa |
| 3879 | 3885 | } |
| 3880 | 3886 | |
| 3881 | 3887 | fn lowerUnnamedConst(self: *Self, tv: TypedValue) InnerError!MCValue { |
| 3882 | log.debug("lowerUnnamedConst: ty = {}, val = {}", .{ tv.ty, tv.val.fmtDebug() }); | |
| 3888 | log.debug("lowerUnnamedConst: ty = {}, val = {}", .{ tv.ty.fmtDebug(), tv.val.fmtDebug() }); | |
| 3883 | 3889 | const local_sym_index = self.bin_file.lowerUnnamedConst(tv, self.mod_fn.owner_decl) catch |err| { |
| 3884 | 3890 | return self.fail("lowering unnamed constant failed: {s}", .{@errorName(err)}); |
| 3885 | 3891 | }; |
| ... | ... | @@ -3907,6 +3913,7 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 3907 | 3913 | if (typed_value.val.castTag(.decl_ref_mut)) |payload| { |
| 3908 | 3914 | return self.lowerDeclRef(typed_value, payload.data.decl); |
| 3909 | 3915 | } |
| 3916 | const target = self.target.*; | |
| 3910 | 3917 | |
| 3911 | 3918 | switch (typed_value.ty.zigTypeTag()) { |
| 3912 | 3919 | .Pointer => switch (typed_value.ty.ptrSize()) { |
| ... | ... | @@ -3916,7 +3923,7 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 3916 | 3923 | else => { |
| 3917 | 3924 | switch (typed_value.val.tag()) { |
| 3918 | 3925 | .int_u64 => { |
| 3919 | return MCValue{ .immediate = typed_value.val.toUnsignedInt() }; | |
| 3926 | return MCValue{ .immediate = typed_value.val.toUnsignedInt(target) }; | |
| 3920 | 3927 | }, |
| 3921 | 3928 | .slice => { |
| 3922 | 3929 | return self.lowerUnnamedConst(typed_value); |
| ... | ... | @@ -3935,7 +3942,7 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 3935 | 3942 | const signed = typed_value.val.toSignedInt(); |
| 3936 | 3943 | break :blk @bitCast(u64, signed); |
| 3937 | 3944 | }, |
| 3938 | .unsigned => typed_value.val.toUnsignedInt(), | |
| 3945 | .unsigned => typed_value.val.toUnsignedInt(target), | |
| 3939 | 3946 | }; |
| 3940 | 3947 | |
| 3941 | 3948 | return MCValue{ .immediate = unsigned }; |
| ... | ... | @@ -4004,20 +4011,20 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 4004 | 4011 | } |
| 4005 | 4012 | |
| 4006 | 4013 | _ = pl; |
| 4007 | return self.fail("TODO implement error union const of type '{}' (non-error)", .{typed_value.ty}); | |
| 4014 | return self.fail("TODO implement error union const of type '{}' (non-error)", .{typed_value.ty.fmtDebug()}); | |
| 4008 | 4015 | } else { |
| 4009 | 4016 | if (!payload_type.hasRuntimeBits()) { |
| 4010 | 4017 | // We use the error type directly as the type. |
| 4011 | 4018 | return self.genTypedValue(.{ .ty = error_type, .val = typed_value.val }); |
| 4012 | 4019 | } |
| 4013 | 4020 | |
| 4014 | return self.fail("TODO implement error union const of type '{}' (error)", .{typed_value.ty}); | |
| 4021 | return self.fail("TODO implement error union const of type '{}' (error)", .{typed_value.ty.fmtDebug()}); | |
| 4015 | 4022 | } |
| 4016 | 4023 | }, |
| 4017 | 4024 | .Struct => { |
| 4018 | 4025 | return self.lowerUnnamedConst(typed_value); |
| 4019 | 4026 | }, |
| 4020 | else => return self.fail("TODO implement const of type '{}'", .{typed_value.ty}), | |
| 4027 | else => return self.fail("TODO implement const of type '{}'", .{typed_value.ty.fmtDebug()}), | |
| 4021 | 4028 | } |
| 4022 | 4029 | } |
| 4023 | 4030 |
src/arch/arm/CodeGen.zig+14-10| ... | ... | @@ -801,8 +801,9 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 801 | 801 | return self.next_stack_offset; |
| 802 | 802 | } |
| 803 | 803 | |
| 804 | const target = self.target.*; | |
| 804 | 805 | const abi_size = math.cast(u32, elem_ty.abiSize(self.target.*)) catch { |
| 805 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty}); | |
| 806 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(target)}); | |
| 806 | 807 | }; |
| 807 | 808 | // TODO swap this for inst.ty.ptrAlign |
| 808 | 809 | const abi_align = elem_ty.abiAlignment(self.target.*); |
| ... | ... | @@ -811,8 +812,9 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 811 | 812 | |
| 812 | 813 | fn allocRegOrMem(self: *Self, inst: Air.Inst.Index, reg_ok: bool) !MCValue { |
| 813 | 814 | const elem_ty = self.air.typeOfIndex(inst); |
| 815 | const target = self.target.*; | |
| 814 | 816 | const abi_size = math.cast(u32, elem_ty.abiSize(self.target.*)) catch { |
| 815 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty}); | |
| 817 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(target)}); | |
| 816 | 818 | }; |
| 817 | 819 | const abi_align = elem_ty.abiAlignment(self.target.*); |
| 818 | 820 | if (abi_align > self.stack_align) |
| ... | ... | @@ -2195,6 +2197,7 @@ fn binOp( |
| 2195 | 2197 | lhs_ty: Type, |
| 2196 | 2198 | rhs_ty: Type, |
| 2197 | 2199 | ) InnerError!MCValue { |
| 2200 | const target = self.target.*; | |
| 2198 | 2201 | switch (tag) { |
| 2199 | 2202 | .add, |
| 2200 | 2203 | .sub, |
| ... | ... | @@ -2204,7 +2207,7 @@ fn binOp( |
| 2204 | 2207 | .Float => return self.fail("TODO ARM binary operations on floats", .{}), |
| 2205 | 2208 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| 2206 | 2209 | .Int => { |
| 2207 | assert(lhs_ty.eql(rhs_ty)); | |
| 2210 | assert(lhs_ty.eql(rhs_ty, target)); | |
| 2208 | 2211 | const int_info = lhs_ty.intInfo(self.target.*); |
| 2209 | 2212 | if (int_info.bits <= 32) { |
| 2210 | 2213 | // Only say yes if the operation is |
| ... | ... | @@ -2245,7 +2248,7 @@ fn binOp( |
| 2245 | 2248 | .Float => return self.fail("TODO ARM binary operations on floats", .{}), |
| 2246 | 2249 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| 2247 | 2250 | .Int => { |
| 2248 | assert(lhs_ty.eql(rhs_ty)); | |
| 2251 | assert(lhs_ty.eql(rhs_ty, target)); | |
| 2249 | 2252 | const int_info = lhs_ty.intInfo(self.target.*); |
| 2250 | 2253 | if (int_info.bits <= 32) { |
| 2251 | 2254 | // TODO add optimisations for multiplication |
| ... | ... | @@ -2299,7 +2302,7 @@ fn binOp( |
| 2299 | 2302 | switch (lhs_ty.zigTypeTag()) { |
| 2300 | 2303 | .Vector => return self.fail("TODO ARM binary operations on vectors", .{}), |
| 2301 | 2304 | .Int => { |
| 2302 | assert(lhs_ty.eql(rhs_ty)); | |
| 2305 | assert(lhs_ty.eql(rhs_ty, target)); | |
| 2303 | 2306 | const int_info = lhs_ty.intInfo(self.target.*); |
| 2304 | 2307 | if (int_info.bits <= 32) { |
| 2305 | 2308 | const lhs_immediate_ok = lhs == .immediate and Instruction.Operand.fromU32(lhs.immediate) != null; |
| ... | ... | @@ -4376,6 +4379,7 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 4376 | 4379 | if (typed_value.val.castTag(.decl_ref_mut)) |payload| { |
| 4377 | 4380 | return self.lowerDeclRef(typed_value, payload.data.decl); |
| 4378 | 4381 | } |
| 4382 | const target = self.target.*; | |
| 4379 | 4383 | |
| 4380 | 4384 | switch (typed_value.ty.zigTypeTag()) { |
| 4381 | 4385 | .Array => { |
| ... | ... | @@ -4388,7 +4392,7 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 4388 | 4392 | else => { |
| 4389 | 4393 | switch (typed_value.val.tag()) { |
| 4390 | 4394 | .int_u64 => { |
| 4391 | return MCValue{ .immediate = @intCast(u32, typed_value.val.toUnsignedInt()) }; | |
| 4395 | return MCValue{ .immediate = @intCast(u32, typed_value.val.toUnsignedInt(target)) }; | |
| 4392 | 4396 | }, |
| 4393 | 4397 | .slice => { |
| 4394 | 4398 | return self.lowerUnnamedConst(typed_value); |
| ... | ... | @@ -4407,7 +4411,7 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 4407 | 4411 | const signed = @intCast(i32, typed_value.val.toSignedInt()); |
| 4408 | 4412 | break :blk @bitCast(u32, signed); |
| 4409 | 4413 | }, |
| 4410 | .unsigned => @intCast(u32, typed_value.val.toUnsignedInt()), | |
| 4414 | .unsigned => @intCast(u32, typed_value.val.toUnsignedInt(target)), | |
| 4411 | 4415 | }; |
| 4412 | 4416 | |
| 4413 | 4417 | return MCValue{ .immediate = unsigned }; |
| ... | ... | @@ -4476,20 +4480,20 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 4476 | 4480 | } |
| 4477 | 4481 | |
| 4478 | 4482 | _ = pl; |
| 4479 | return self.fail("TODO implement error union const of type '{}' (non-error)", .{typed_value.ty}); | |
| 4483 | return self.fail("TODO implement error union const of type '{}' (non-error)", .{typed_value.ty.fmtDebug()}); | |
| 4480 | 4484 | } else { |
| 4481 | 4485 | if (!payload_type.hasRuntimeBits()) { |
| 4482 | 4486 | // We use the error type directly as the type. |
| 4483 | 4487 | return self.genTypedValue(.{ .ty = error_type, .val = typed_value.val }); |
| 4484 | 4488 | } |
| 4485 | 4489 | |
| 4486 | return self.fail("TODO implement error union const of type '{}' (error)", .{typed_value.ty}); | |
| 4490 | return self.fail("TODO implement error union const of type '{}' (error)", .{typed_value.ty.fmtDebug()}); | |
| 4487 | 4491 | } |
| 4488 | 4492 | }, |
| 4489 | 4493 | .Struct => { |
| 4490 | 4494 | return self.lowerUnnamedConst(typed_value); |
| 4491 | 4495 | }, |
| 4492 | else => return self.fail("TODO implement const of type '{}'", .{typed_value.ty}), | |
| 4496 | else => return self.fail("TODO implement const of type '{}'", .{typed_value.ty.fmtDebug()}), | |
| 4493 | 4497 | } |
| 4494 | 4498 | } |
| 4495 | 4499 |
src/arch/arm/Emit.zig+3-2| ... | ... | @@ -384,7 +384,7 @@ fn addDbgInfoTypeReloc(self: *Emit, ty: Type) !void { |
| 384 | 384 | const index = dbg_out.dbg_info.items.len; |
| 385 | 385 | try dbg_out.dbg_info.resize(index + 4); // DW.AT.type, DW.FORM.ref4 |
| 386 | 386 | |
| 387 | const gop = try dbg_out.dbg_info_type_relocs.getOrPut(self.bin_file.allocator, ty); | |
| 387 | const gop = try dbg_out.dbg_info_type_relocs.getOrPutContext(self.bin_file.allocator, ty, .{ .target = self.target.* }); | |
| 388 | 388 | if (!gop.found_existing) { |
| 389 | 389 | gop.value_ptr.* = .{ |
| 390 | 390 | .off = undefined, |
| ... | ... | @@ -404,6 +404,7 @@ fn genArgDbgInfo(self: *Emit, inst: Air.Inst.Index, arg_index: u32) !void { |
| 404 | 404 | const ty = self.function.air.instructions.items(.data)[inst].ty; |
| 405 | 405 | const name = self.function.mod_fn.getParamName(arg_index); |
| 406 | 406 | const name_with_null = name.ptr[0 .. name.len + 1]; |
| 407 | const target = self.target.*; | |
| 407 | 408 | |
| 408 | 409 | switch (mcv) { |
| 409 | 410 | .register => |reg| { |
| ... | ... | @@ -429,7 +430,7 @@ fn genArgDbgInfo(self: *Emit, inst: Air.Inst.Index, arg_index: u32) !void { |
| 429 | 430 | switch (self.debug_output) { |
| 430 | 431 | .dwarf => |dbg_out| { |
| 431 | 432 | const abi_size = math.cast(u32, ty.abiSize(self.target.*)) catch { |
| 432 | return self.fail("type '{}' too big to fit into stack frame", .{ty}); | |
| 433 | return self.fail("type '{}' too big to fit into stack frame", .{ty.fmt(target)}); | |
| 433 | 434 | }; |
| 434 | 435 | const adjusted_stack_offset = switch (mcv) { |
| 435 | 436 | .stack_offset => |offset| math.negateCast(offset + abi_size) catch { |
src/arch/riscv64/CodeGen.zig+15-10| ... | ... | @@ -749,7 +749,9 @@ fn addDbgInfoTypeReloc(self: *Self, ty: Type) !void { |
| 749 | 749 | const index = dbg_out.dbg_info.items.len; |
| 750 | 750 | try dbg_out.dbg_info.resize(index + 4); // DW.AT.type, DW.FORM.ref4 |
| 751 | 751 | |
| 752 | const gop = try dbg_out.dbg_info_type_relocs.getOrPut(self.gpa, ty); | |
| 752 | const gop = try dbg_out.dbg_info_type_relocs.getOrPutContext(self.gpa, ty, .{ | |
| 753 | .target = self.target.*, | |
| 754 | }); | |
| 753 | 755 | if (!gop.found_existing) { |
| 754 | 756 | gop.value_ptr.* = .{ |
| 755 | 757 | .off = undefined, |
| ... | ... | @@ -781,8 +783,9 @@ fn allocMem(self: *Self, inst: Air.Inst.Index, abi_size: u32, abi_align: u32) !u |
| 781 | 783 | /// Use a pointer instruction as the basis for allocating stack memory. |
| 782 | 784 | fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 783 | 785 | const elem_ty = self.air.typeOfIndex(inst).elemType(); |
| 786 | const target = self.target.*; | |
| 784 | 787 | const abi_size = math.cast(u32, elem_ty.abiSize(self.target.*)) catch { |
| 785 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty}); | |
| 788 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(target)}); | |
| 786 | 789 | }; |
| 787 | 790 | // TODO swap this for inst.ty.ptrAlign |
| 788 | 791 | const abi_align = elem_ty.abiAlignment(self.target.*); |
| ... | ... | @@ -791,8 +794,9 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 791 | 794 | |
| 792 | 795 | fn allocRegOrMem(self: *Self, inst: Air.Inst.Index, reg_ok: bool) !MCValue { |
| 793 | 796 | const elem_ty = self.air.typeOfIndex(inst); |
| 797 | const target = self.target.*; | |
| 794 | 798 | const abi_size = math.cast(u32, elem_ty.abiSize(self.target.*)) catch { |
| 795 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty}); | |
| 799 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(target)}); | |
| 796 | 800 | }; |
| 797 | 801 | const abi_align = elem_ty.abiAlignment(self.target.*); |
| 798 | 802 | if (abi_align > self.stack_align) |
| ... | ... | @@ -1048,7 +1052,7 @@ fn binOp( |
| 1048 | 1052 | .Float => return self.fail("TODO binary operations on floats", .{}), |
| 1049 | 1053 | .Vector => return self.fail("TODO binary operations on vectors", .{}), |
| 1050 | 1054 | .Int => { |
| 1051 | assert(lhs_ty.eql(rhs_ty)); | |
| 1055 | assert(lhs_ty.eql(rhs_ty, self.target.*)); | |
| 1052 | 1056 | const int_info = lhs_ty.intInfo(self.target.*); |
| 1053 | 1057 | if (int_info.bits <= 64) { |
| 1054 | 1058 | // TODO immediate operands |
| ... | ... | @@ -1778,7 +1782,7 @@ fn airCmp(self: *Self, inst: Air.Inst.Index, op: math.CompareOperator) !void { |
| 1778 | 1782 | if (self.liveness.isUnused(inst)) |
| 1779 | 1783 | return self.finishAir(inst, .dead, .{ bin_op.lhs, bin_op.rhs, .none }); |
| 1780 | 1784 | const ty = self.air.typeOf(bin_op.lhs); |
| 1781 | assert(ty.eql(self.air.typeOf(bin_op.rhs))); | |
| 1785 | assert(ty.eql(self.air.typeOf(bin_op.rhs), self.target.*)); | |
| 1782 | 1786 | if (ty.zigTypeTag() == .ErrorSet) |
| 1783 | 1787 | return self.fail("TODO implement cmp for errors", .{}); |
| 1784 | 1788 | |
| ... | ... | @@ -2531,6 +2535,7 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 2531 | 2535 | if (typed_value.val.castTag(.decl_ref_mut)) |payload| { |
| 2532 | 2536 | return self.lowerDeclRef(typed_value, payload.data.decl); |
| 2533 | 2537 | } |
| 2538 | const target = self.target.*; | |
| 2534 | 2539 | const ptr_bits = self.target.cpu.arch.ptrBitWidth(); |
| 2535 | 2540 | switch (typed_value.ty.zigTypeTag()) { |
| 2536 | 2541 | .Pointer => switch (typed_value.ty.ptrSize()) { |
| ... | ... | @@ -2538,7 +2543,7 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 2538 | 2543 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 2539 | 2544 | const ptr_type = typed_value.ty.slicePtrFieldType(&buf); |
| 2540 | 2545 | const ptr_mcv = try self.genTypedValue(.{ .ty = ptr_type, .val = typed_value.val }); |
| 2541 | const slice_len = typed_value.val.sliceLen(); | |
| 2546 | const slice_len = typed_value.val.sliceLen(target); | |
| 2542 | 2547 | // Codegen can't handle some kinds of indirection. If the wrong union field is accessed here it may mean |
| 2543 | 2548 | // the Sema code needs to use anonymous Decls or alloca instructions to store data. |
| 2544 | 2549 | const ptr_imm = ptr_mcv.memory; |
| ... | ... | @@ -2549,7 +2554,7 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 2549 | 2554 | }, |
| 2550 | 2555 | else => { |
| 2551 | 2556 | if (typed_value.val.tag() == .int_u64) { |
| 2552 | return MCValue{ .immediate = typed_value.val.toUnsignedInt() }; | |
| 2557 | return MCValue{ .immediate = typed_value.val.toUnsignedInt(target) }; | |
| 2553 | 2558 | } |
| 2554 | 2559 | return self.fail("TODO codegen more kinds of const pointers", .{}); |
| 2555 | 2560 | }, |
| ... | ... | @@ -2559,7 +2564,7 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 2559 | 2564 | if (info.bits > ptr_bits or info.signedness == .signed) { |
| 2560 | 2565 | return self.fail("TODO const int bigger than ptr and signed int", .{}); |
| 2561 | 2566 | } |
| 2562 | return MCValue{ .immediate = typed_value.val.toUnsignedInt() }; | |
| 2567 | return MCValue{ .immediate = typed_value.val.toUnsignedInt(target) }; | |
| 2563 | 2568 | }, |
| 2564 | 2569 | .Bool => { |
| 2565 | 2570 | return MCValue{ .immediate = @boolToInt(typed_value.val.toBool()) }; |
| ... | ... | @@ -2629,9 +2634,9 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 2629 | 2634 | return self.genTypedValue(.{ .ty = error_type, .val = sub_val }); |
| 2630 | 2635 | } |
| 2631 | 2636 | |
| 2632 | return self.fail("TODO implement error union const of type '{}'", .{typed_value.ty}); | |
| 2637 | return self.fail("TODO implement error union const of type '{}'", .{typed_value.ty.fmtDebug()}); | |
| 2633 | 2638 | }, |
| 2634 | else => return self.fail("TODO implement const of type '{}'", .{typed_value.ty}), | |
| 2639 | else => return self.fail("TODO implement const of type '{}'", .{typed_value.ty.fmtDebug()}), | |
| 2635 | 2640 | } |
| 2636 | 2641 | } |
| 2637 | 2642 |
src/arch/wasm/CodeGen.zig+21-14| ... | ... | @@ -1021,7 +1021,9 @@ fn allocStack(self: *Self, ty: Type) !WValue { |
| 1021 | 1021 | } |
| 1022 | 1022 | |
| 1023 | 1023 | const abi_size = std.math.cast(u32, ty.abiSize(self.target)) catch { |
| 1024 | return self.fail("Type {} with ABI size of {d} exceeds stack frame size", .{ ty, ty.abiSize(self.target) }); | |
| 1024 | return self.fail("Type {} with ABI size of {d} exceeds stack frame size", .{ | |
| 1025 | ty.fmt(self.target), ty.abiSize(self.target), | |
| 1026 | }); | |
| 1025 | 1027 | }; |
| 1026 | 1028 | const abi_align = ty.abiAlignment(self.target); |
| 1027 | 1029 | |
| ... | ... | @@ -1053,7 +1055,9 @@ fn allocStackPtr(self: *Self, inst: Air.Inst.Index) !WValue { |
| 1053 | 1055 | |
| 1054 | 1056 | const abi_alignment = ptr_ty.ptrAlignment(self.target); |
| 1055 | 1057 | const abi_size = std.math.cast(u32, pointee_ty.abiSize(self.target)) catch { |
| 1056 | return self.fail("Type {} with ABI size of {d} exceeds stack frame size", .{ pointee_ty, pointee_ty.abiSize(self.target) }); | |
| 1058 | return self.fail("Type {} with ABI size of {d} exceeds stack frame size", .{ | |
| 1059 | pointee_ty.fmt(self.target), pointee_ty.abiSize(self.target), | |
| 1060 | }); | |
| 1057 | 1061 | }; |
| 1058 | 1062 | if (abi_alignment > self.stack_alignment) { |
| 1059 | 1063 | self.stack_alignment = abi_alignment; |
| ... | ... | @@ -1750,7 +1754,7 @@ fn airBinOp(self: *Self, inst: Air.Inst.Index, op: Op) InnerError!WValue { |
| 1750 | 1754 | const operand_ty = self.air.typeOfIndex(inst); |
| 1751 | 1755 | |
| 1752 | 1756 | if (isByRef(operand_ty, self.target)) { |
| 1753 | return self.fail("TODO: Implement binary operation for type: {}", .{operand_ty}); | |
| 1757 | return self.fail("TODO: Implement binary operation for type: {}", .{operand_ty.fmtDebug()}); | |
| 1754 | 1758 | } |
| 1755 | 1759 | |
| 1756 | 1760 | try self.emitWValue(lhs); |
| ... | ... | @@ -1918,6 +1922,8 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 1918 | 1922 | return self.lowerDeclRefValue(.{ .ty = ty, .val = val }, decl); |
| 1919 | 1923 | } |
| 1920 | 1924 | |
| 1925 | const target = self.target; | |
| 1926 | ||
| 1921 | 1927 | switch (ty.zigTypeTag()) { |
| 1922 | 1928 | .Int => { |
| 1923 | 1929 | const int_info = ty.intInfo(self.target); |
| ... | ... | @@ -1929,13 +1935,13 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 1929 | 1935 | else => unreachable, |
| 1930 | 1936 | }, |
| 1931 | 1937 | .unsigned => switch (int_info.bits) { |
| 1932 | 0...32 => return WValue{ .imm32 = @intCast(u32, val.toUnsignedInt()) }, | |
| 1933 | 33...64 => return WValue{ .imm64 = val.toUnsignedInt() }, | |
| 1938 | 0...32 => return WValue{ .imm32 = @intCast(u32, val.toUnsignedInt(target)) }, | |
| 1939 | 33...64 => return WValue{ .imm64 = val.toUnsignedInt(target) }, | |
| 1934 | 1940 | else => unreachable, |
| 1935 | 1941 | }, |
| 1936 | 1942 | } |
| 1937 | 1943 | }, |
| 1938 | .Bool => return WValue{ .imm32 = @intCast(u32, val.toUnsignedInt()) }, | |
| 1944 | .Bool => return WValue{ .imm32 = @intCast(u32, val.toUnsignedInt(target)) }, | |
| 1939 | 1945 | .Float => switch (ty.floatBits(self.target)) { |
| 1940 | 1946 | 0...32 => return WValue{ .float32 = val.toFloat(f32) }, |
| 1941 | 1947 | 33...64 => return WValue{ .float64 = val.toFloat(f64) }, |
| ... | ... | @@ -1945,7 +1951,7 @@ fn lowerConstant(self: *Self, val: Value, ty: Type) InnerError!WValue { |
| 1945 | 1951 | .field_ptr, .elem_ptr => { |
| 1946 | 1952 | return self.lowerParentPtr(val, ty.childType()); |
| 1947 | 1953 | }, |
| 1948 | .int_u64, .one => return WValue{ .imm32 = @intCast(u32, val.toUnsignedInt()) }, | |
| 1954 | .int_u64, .one => return WValue{ .imm32 = @intCast(u32, val.toUnsignedInt(target)) }, | |
| 1949 | 1955 | .zero, .null_value => return WValue{ .imm32 = 0 }, |
| 1950 | 1956 | else => return self.fail("Wasm TODO: lowerConstant for other const pointer tag {s}", .{val.tag()}), |
| 1951 | 1957 | }, |
| ... | ... | @@ -2044,6 +2050,7 @@ fn emitUndefined(self: *Self, ty: Type) InnerError!WValue { |
| 2044 | 2050 | /// It's illegal to provide a value with a type that cannot be represented |
| 2045 | 2051 | /// as an integer value. |
| 2046 | 2052 | fn valueAsI32(self: Self, val: Value, ty: Type) i32 { |
| 2053 | const target = self.target; | |
| 2047 | 2054 | switch (ty.zigTypeTag()) { |
| 2048 | 2055 | .Enum => { |
| 2049 | 2056 | if (val.castTag(.enum_field_index)) |field_index| { |
| ... | ... | @@ -2071,7 +2078,7 @@ fn valueAsI32(self: Self, val: Value, ty: Type) i32 { |
| 2071 | 2078 | }, |
| 2072 | 2079 | .Int => switch (ty.intInfo(self.target).signedness) { |
| 2073 | 2080 | .signed => return @truncate(i32, val.toSignedInt()), |
| 2074 | .unsigned => return @bitCast(i32, @truncate(u32, val.toUnsignedInt())), | |
| 2081 | .unsigned => return @bitCast(i32, @truncate(u32, val.toUnsignedInt(target))), | |
| 2075 | 2082 | }, |
| 2076 | 2083 | .ErrorSet => { |
| 2077 | 2084 | const kv = self.bin_file.base.options.module.?.getErrorValue(val.getError().?) catch unreachable; // passed invalid `Value` to function |
| ... | ... | @@ -2296,7 +2303,7 @@ fn airStructFieldPtr(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2296 | 2303 | const struct_ty = self.air.typeOf(extra.data.struct_operand).childType(); |
| 2297 | 2304 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(extra.data.field_index, self.target)) catch { |
| 2298 | 2305 | return self.fail("Field type '{}' too big to fit into stack frame", .{ |
| 2299 | struct_ty.structFieldType(extra.data.field_index), | |
| 2306 | struct_ty.structFieldType(extra.data.field_index).fmt(self.target), | |
| 2300 | 2307 | }); |
| 2301 | 2308 | }; |
| 2302 | 2309 | return self.structFieldPtr(struct_ptr, offset); |
| ... | ... | @@ -2309,7 +2316,7 @@ fn airStructFieldPtrIndex(self: *Self, inst: Air.Inst.Index, index: u32) InnerEr |
| 2309 | 2316 | const field_ty = struct_ty.structFieldType(index); |
| 2310 | 2317 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(index, self.target)) catch { |
| 2311 | 2318 | return self.fail("Field type '{}' too big to fit into stack frame", .{ |
| 2312 | field_ty, | |
| 2319 | field_ty.fmt(self.target), | |
| 2313 | 2320 | }); |
| 2314 | 2321 | }; |
| 2315 | 2322 | return self.structFieldPtr(struct_ptr, offset); |
| ... | ... | @@ -2335,7 +2342,7 @@ fn airStructFieldVal(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2335 | 2342 | const field_ty = struct_ty.structFieldType(field_index); |
| 2336 | 2343 | if (!field_ty.hasRuntimeBits()) return WValue{ .none = {} }; |
| 2337 | 2344 | const offset = std.math.cast(u32, struct_ty.structFieldOffset(field_index, self.target)) catch { |
| 2338 | return self.fail("Field type '{}' too big to fit into stack frame", .{field_ty}); | |
| 2345 | return self.fail("Field type '{}' too big to fit into stack frame", .{field_ty.fmt(self.target)}); | |
| 2339 | 2346 | }; |
| 2340 | 2347 | |
| 2341 | 2348 | if (isByRef(field_ty, self.target)) { |
| ... | ... | @@ -2716,7 +2723,7 @@ fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue |
| 2716 | 2723 | var buf: Type.Payload.ElemType = undefined; |
| 2717 | 2724 | const payload_ty = opt_ty.optionalChild(&buf); |
| 2718 | 2725 | if (!payload_ty.hasRuntimeBits()) { |
| 2719 | return self.fail("TODO: Implement OptionalPayloadPtrSet for optional with zero-sized type {}", .{payload_ty}); | |
| 2726 | return self.fail("TODO: Implement OptionalPayloadPtrSet for optional with zero-sized type {}", .{payload_ty.fmtDebug()}); | |
| 2720 | 2727 | } |
| 2721 | 2728 | |
| 2722 | 2729 | if (opt_ty.isPtrLikeOptional()) { |
| ... | ... | @@ -2724,7 +2731,7 @@ fn airOptionalPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!WValue |
| 2724 | 2731 | } |
| 2725 | 2732 | |
| 2726 | 2733 | const offset = std.math.cast(u32, opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target)) catch { |
| 2727 | return self.fail("Optional type {} too big to fit into stack frame", .{opt_ty}); | |
| 2734 | return self.fail("Optional type {} too big to fit into stack frame", .{opt_ty.fmt(self.target)}); | |
| 2728 | 2735 | }; |
| 2729 | 2736 | |
| 2730 | 2737 | try self.emitWValue(operand); |
| ... | ... | @@ -2753,7 +2760,7 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!WValue { |
| 2753 | 2760 | return operand; |
| 2754 | 2761 | } |
| 2755 | 2762 | const offset = std.math.cast(u32, op_ty.abiSize(self.target) - payload_ty.abiSize(self.target)) catch { |
| 2756 | return self.fail("Optional type {} too big to fit into stack frame", .{op_ty}); | |
| 2763 | return self.fail("Optional type {} too big to fit into stack frame", .{op_ty.fmt(self.target)}); | |
| 2757 | 2764 | }; |
| 2758 | 2765 | |
| 2759 | 2766 | // Create optional type, set the non-null bit, and store the operand inside the optional type |
src/arch/x86_64/CodeGen.zig+12-8| ... | ... | @@ -892,8 +892,9 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 892 | 892 | return self.allocMem(inst, @sizeOf(usize), @alignOf(usize)); |
| 893 | 893 | } |
| 894 | 894 | |
| 895 | const target = self.target.*; | |
| 895 | 896 | const abi_size = math.cast(u32, elem_ty.abiSize(self.target.*)) catch { |
| 896 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty}); | |
| 897 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(target)}); | |
| 897 | 898 | }; |
| 898 | 899 | // TODO swap this for inst.ty.ptrAlign |
| 899 | 900 | const abi_align = ptr_ty.ptrAlignment(self.target.*); |
| ... | ... | @@ -902,8 +903,9 @@ fn allocMemPtr(self: *Self, inst: Air.Inst.Index) !u32 { |
| 902 | 903 | |
| 903 | 904 | fn allocRegOrMem(self: *Self, inst: Air.Inst.Index, reg_ok: bool) !MCValue { |
| 904 | 905 | const elem_ty = self.air.typeOfIndex(inst); |
| 906 | const target = self.target.*; | |
| 905 | 907 | const abi_size = math.cast(u32, elem_ty.abiSize(self.target.*)) catch { |
| 906 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty}); | |
| 908 | return self.fail("type '{}' too big to fit into stack frame", .{elem_ty.fmt(target)}); | |
| 907 | 909 | }; |
| 908 | 910 | const abi_align = elem_ty.abiAlignment(self.target.*); |
| 909 | 911 | if (abi_align > self.stack_align) |
| ... | ... | @@ -1142,7 +1144,7 @@ fn airMin(self: *Self, inst: Air.Inst.Index) !void { |
| 1142 | 1144 | |
| 1143 | 1145 | const ty = self.air.typeOfIndex(inst); |
| 1144 | 1146 | if (ty.zigTypeTag() != .Int) { |
| 1145 | return self.fail("TODO implement min for type {}", .{ty}); | |
| 1147 | return self.fail("TODO implement min for type {}", .{ty.fmtDebug()}); | |
| 1146 | 1148 | } |
| 1147 | 1149 | const signedness = ty.intInfo(self.target.*).signedness; |
| 1148 | 1150 | const result: MCValue = result: { |
| ... | ... | @@ -1676,13 +1678,13 @@ fn airShl(self: *Self, inst: Air.Inst.Index) !void { |
| 1676 | 1678 | const ty = self.air.typeOfIndex(inst); |
| 1677 | 1679 | const tag = self.air.instructions.items(.tag)[inst]; |
| 1678 | 1680 | switch (tag) { |
| 1679 | .shl_exact => return self.fail("TODO implement {} for type {}", .{ tag, ty }), | |
| 1681 | .shl_exact => return self.fail("TODO implement {} for type {}", .{ tag, ty.fmtDebug() }), | |
| 1680 | 1682 | .shl => {}, |
| 1681 | 1683 | else => unreachable, |
| 1682 | 1684 | } |
| 1683 | 1685 | |
| 1684 | 1686 | if (ty.zigTypeTag() != .Int) { |
| 1685 | return self.fail("TODO implement .shl for type {}", .{ty}); | |
| 1687 | return self.fail("TODO implement .shl for type {}", .{ty.fmtDebug()}); | |
| 1686 | 1688 | } |
| 1687 | 1689 | if (ty.abiSize(self.target.*) > 8) { |
| 1688 | 1690 | return self.fail("TODO implement .shl for integers larger than 8 bytes", .{}); |
| ... | ... | @@ -5820,7 +5822,7 @@ fn lowerDeclRef(self: *Self, tv: TypedValue, decl: *Module.Decl) InnerError!MCVa |
| 5820 | 5822 | } |
| 5821 | 5823 | |
| 5822 | 5824 | fn lowerUnnamedConst(self: *Self, tv: TypedValue) InnerError!MCValue { |
| 5823 | log.debug("lowerUnnamedConst: ty = {}, val = {}", .{ tv.ty, tv.val.fmtDebug() }); | |
| 5825 | log.debug("lowerUnnamedConst: ty = {}, val = {}", .{ tv.ty.fmtDebug(), tv.val.fmtDebug() }); | |
| 5824 | 5826 | const local_sym_index = self.bin_file.lowerUnnamedConst(tv, self.mod_fn.owner_decl) catch |err| { |
| 5825 | 5827 | return self.fail("lowering unnamed constant failed: {s}", .{@errorName(err)}); |
| 5826 | 5828 | }; |
| ... | ... | @@ -5850,13 +5852,15 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 5850 | 5852 | return self.lowerDeclRef(typed_value, payload.data.decl); |
| 5851 | 5853 | } |
| 5852 | 5854 | |
| 5855 | const target = self.target.*; | |
| 5856 | ||
| 5853 | 5857 | switch (typed_value.ty.zigTypeTag()) { |
| 5854 | 5858 | .Pointer => switch (typed_value.ty.ptrSize()) { |
| 5855 | 5859 | .Slice => {}, |
| 5856 | 5860 | else => { |
| 5857 | 5861 | switch (typed_value.val.tag()) { |
| 5858 | 5862 | .int_u64 => { |
| 5859 | return MCValue{ .immediate = typed_value.val.toUnsignedInt() }; | |
| 5863 | return MCValue{ .immediate = typed_value.val.toUnsignedInt(target) }; | |
| 5860 | 5864 | }, |
| 5861 | 5865 | else => {}, |
| 5862 | 5866 | } |
| ... | ... | @@ -5868,7 +5872,7 @@ fn genTypedValue(self: *Self, typed_value: TypedValue) InnerError!MCValue { |
| 5868 | 5872 | return MCValue{ .immediate = @bitCast(u64, typed_value.val.toSignedInt()) }; |
| 5869 | 5873 | } |
| 5870 | 5874 | if (!(info.bits > ptr_bits or info.signedness == .signed)) { |
| 5871 | return MCValue{ .immediate = typed_value.val.toUnsignedInt() }; | |
| 5875 | return MCValue{ .immediate = typed_value.val.toUnsignedInt(target) }; | |
| 5872 | 5876 | } |
| 5873 | 5877 | }, |
| 5874 | 5878 | .Bool => { |
src/arch/x86_64/Emit.zig+3-1| ... | ... | @@ -1118,7 +1118,9 @@ fn addDbgInfoTypeReloc(emit: *Emit, ty: Type) !void { |
| 1118 | 1118 | const index = dbg_out.dbg_info.items.len; |
| 1119 | 1119 | try dbg_out.dbg_info.resize(index + 4); // DW.AT.type, DW.FORM.ref4 |
| 1120 | 1120 | |
| 1121 | const gop = try dbg_out.dbg_info_type_relocs.getOrPut(emit.bin_file.allocator, ty); | |
| 1121 | const gop = try dbg_out.dbg_info_type_relocs.getOrPutContext(emit.bin_file.allocator, ty, .{ | |
| 1122 | .target = emit.target.*, | |
| 1123 | }); | |
| 1122 | 1124 | if (!gop.found_existing) { |
| 1123 | 1125 | gop.value_ptr.* = .{ |
| 1124 | 1126 | .off = undefined, |
src/codegen.zig+19-16| ... | ... | @@ -165,7 +165,10 @@ pub fn generateSymbol( |
| 165 | 165 | const target = bin_file.options.target; |
| 166 | 166 | const endian = target.cpu.arch.endian(); |
| 167 | 167 | |
| 168 | log.debug("generateSymbol: ty = {}, val = {}", .{ typed_value.ty, typed_value.val.fmtDebug() }); | |
| 168 | log.debug("generateSymbol: ty = {}, val = {}", .{ | |
| 169 | typed_value.ty.fmtDebug(), | |
| 170 | typed_value.val.fmtDebug(), | |
| 171 | }); | |
| 169 | 172 | |
| 170 | 173 | if (typed_value.val.isUndefDeep()) { |
| 171 | 174 | const abi_size = try math.cast(usize, typed_value.ty.abiSize(target)); |
| ... | ... | @@ -295,11 +298,11 @@ pub fn generateSymbol( |
| 295 | 298 | .zero, .one, .int_u64, .int_big_positive => { |
| 296 | 299 | switch (target.cpu.arch.ptrBitWidth()) { |
| 297 | 300 | 32 => { |
| 298 | const x = typed_value.val.toUnsignedInt(); | |
| 301 | const x = typed_value.val.toUnsignedInt(target); | |
| 299 | 302 | mem.writeInt(u32, try code.addManyAsArray(4), @intCast(u32, x), endian); |
| 300 | 303 | }, |
| 301 | 304 | 64 => { |
| 302 | const x = typed_value.val.toUnsignedInt(); | |
| 305 | const x = typed_value.val.toUnsignedInt(target); | |
| 303 | 306 | mem.writeInt(u64, try code.addManyAsArray(8), x, endian); |
| 304 | 307 | }, |
| 305 | 308 | else => unreachable, |
| ... | ... | @@ -433,7 +436,7 @@ pub fn generateSymbol( |
| 433 | 436 | // TODO populate .debug_info for the integer |
| 434 | 437 | const info = typed_value.ty.intInfo(bin_file.options.target); |
| 435 | 438 | if (info.bits <= 8) { |
| 436 | const x = @intCast(u8, typed_value.val.toUnsignedInt()); | |
| 439 | const x = @intCast(u8, typed_value.val.toUnsignedInt(target)); | |
| 437 | 440 | try code.append(x); |
| 438 | 441 | return Result{ .appended = {} }; |
| 439 | 442 | } |
| ... | ... | @@ -443,20 +446,20 @@ pub fn generateSymbol( |
| 443 | 446 | bin_file.allocator, |
| 444 | 447 | src_loc, |
| 445 | 448 | "TODO implement generateSymbol for big ints ('{}')", |
| 446 | .{typed_value.ty}, | |
| 449 | .{typed_value.ty.fmtDebug()}, | |
| 447 | 450 | ), |
| 448 | 451 | }; |
| 449 | 452 | } |
| 450 | 453 | switch (info.signedness) { |
| 451 | 454 | .unsigned => { |
| 452 | 455 | if (info.bits <= 16) { |
| 453 | const x = @intCast(u16, typed_value.val.toUnsignedInt()); | |
| 456 | const x = @intCast(u16, typed_value.val.toUnsignedInt(target)); | |
| 454 | 457 | mem.writeInt(u16, try code.addManyAsArray(2), x, endian); |
| 455 | 458 | } else if (info.bits <= 32) { |
| 456 | const x = @intCast(u32, typed_value.val.toUnsignedInt()); | |
| 459 | const x = @intCast(u32, typed_value.val.toUnsignedInt(target)); | |
| 457 | 460 | mem.writeInt(u32, try code.addManyAsArray(4), x, endian); |
| 458 | 461 | } else { |
| 459 | const x = typed_value.val.toUnsignedInt(); | |
| 462 | const x = typed_value.val.toUnsignedInt(target); | |
| 460 | 463 | mem.writeInt(u64, try code.addManyAsArray(8), x, endian); |
| 461 | 464 | } |
| 462 | 465 | }, |
| ... | ... | @@ -482,7 +485,7 @@ pub fn generateSymbol( |
| 482 | 485 | |
| 483 | 486 | const info = typed_value.ty.intInfo(target); |
| 484 | 487 | if (info.bits <= 8) { |
| 485 | const x = @intCast(u8, int_val.toUnsignedInt()); | |
| 488 | const x = @intCast(u8, int_val.toUnsignedInt(target)); | |
| 486 | 489 | try code.append(x); |
| 487 | 490 | return Result{ .appended = {} }; |
| 488 | 491 | } |
| ... | ... | @@ -492,20 +495,20 @@ pub fn generateSymbol( |
| 492 | 495 | bin_file.allocator, |
| 493 | 496 | src_loc, |
| 494 | 497 | "TODO implement generateSymbol for big int enums ('{}')", |
| 495 | .{typed_value.ty}, | |
| 498 | .{typed_value.ty.fmtDebug()}, | |
| 496 | 499 | ), |
| 497 | 500 | }; |
| 498 | 501 | } |
| 499 | 502 | switch (info.signedness) { |
| 500 | 503 | .unsigned => { |
| 501 | 504 | if (info.bits <= 16) { |
| 502 | const x = @intCast(u16, int_val.toUnsignedInt()); | |
| 505 | const x = @intCast(u16, int_val.toUnsignedInt(target)); | |
| 503 | 506 | mem.writeInt(u16, try code.addManyAsArray(2), x, endian); |
| 504 | 507 | } else if (info.bits <= 32) { |
| 505 | const x = @intCast(u32, int_val.toUnsignedInt()); | |
| 508 | const x = @intCast(u32, int_val.toUnsignedInt(target)); | |
| 506 | 509 | mem.writeInt(u32, try code.addManyAsArray(4), x, endian); |
| 507 | 510 | } else { |
| 508 | const x = int_val.toUnsignedInt(); | |
| 511 | const x = int_val.toUnsignedInt(target); | |
| 509 | 512 | mem.writeInt(u64, try code.addManyAsArray(8), x, endian); |
| 510 | 513 | } |
| 511 | 514 | }, |
| ... | ... | @@ -597,7 +600,7 @@ pub fn generateSymbol( |
| 597 | 600 | } |
| 598 | 601 | |
| 599 | 602 | const union_ty = typed_value.ty.cast(Type.Payload.Union).?.data; |
| 600 | const field_index = union_ty.tag_ty.enumTagFieldIndex(union_obj.tag).?; | |
| 603 | const field_index = union_ty.tag_ty.enumTagFieldIndex(union_obj.tag, target).?; | |
| 601 | 604 | assert(union_ty.haveFieldTypes()); |
| 602 | 605 | const field_ty = union_ty.fields.values()[field_index].ty; |
| 603 | 606 | if (!field_ty.hasRuntimeBits()) { |
| ... | ... | @@ -787,6 +790,7 @@ fn lowerDeclRef( |
| 787 | 790 | debug_output: DebugInfoOutput, |
| 788 | 791 | reloc_info: RelocInfo, |
| 789 | 792 | ) GenerateSymbolError!Result { |
| 793 | const target = bin_file.options.target; | |
| 790 | 794 | if (typed_value.ty.isSlice()) { |
| 791 | 795 | // generate ptr |
| 792 | 796 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| ... | ... | @@ -805,7 +809,7 @@ fn lowerDeclRef( |
| 805 | 809 | // generate length |
| 806 | 810 | var slice_len: Value.Payload.U64 = .{ |
| 807 | 811 | .base = .{ .tag = .int_u64 }, |
| 808 | .data = typed_value.val.sliceLen(), | |
| 812 | .data = typed_value.val.sliceLen(target), | |
| 809 | 813 | }; |
| 810 | 814 | switch (try generateSymbol(bin_file, src_loc, .{ |
| 811 | 815 | .ty = Type.usize, |
| ... | ... | @@ -821,7 +825,6 @@ fn lowerDeclRef( |
| 821 | 825 | return Result{ .appended = {} }; |
| 822 | 826 | } |
| 823 | 827 | |
| 824 | const target = bin_file.options.target; | |
| 825 | 828 | const ptr_width = target.cpu.arch.ptrBitWidth(); |
| 826 | 829 | const is_fn_body = decl.ty.zigTypeTag() == .Fn; |
| 827 | 830 | if (!is_fn_body and !decl.ty.hasRuntimeBits()) { |
src/codegen/c.zig+36-21| ... | ... | @@ -56,8 +56,14 @@ pub const TypedefMap = std.ArrayHashMap( |
| 56 | 56 | true, |
| 57 | 57 | ); |
| 58 | 58 | |
| 59 | const FormatTypeAsCIdentContext = struct { | |
| 60 | ty: Type, | |
| 61 | target: std.Target, | |
| 62 | }; | |
| 63 | ||
| 64 | /// TODO make this not cut off at 128 bytes | |
| 59 | 65 | fn formatTypeAsCIdentifier( |
| 60 | data: Type, | |
| 66 | data: FormatTypeAsCIdentContext, | |
| 61 | 67 | comptime fmt: []const u8, |
| 62 | 68 | options: std.fmt.FormatOptions, |
| 63 | 69 | writer: anytype, |
| ... | ... | @@ -65,13 +71,15 @@ fn formatTypeAsCIdentifier( |
| 65 | 71 | _ = fmt; |
| 66 | 72 | _ = options; |
| 67 | 73 | var buffer = [1]u8{0} ** 128; |
| 68 | // We don't care if it gets cut off, it's still more unique than a number | |
| 69 | var buf = std.fmt.bufPrint(&buffer, "{}", .{data}) catch &buffer; | |
| 74 | var buf = std.fmt.bufPrint(&buffer, "{}", .{data.ty.fmt(data.target)}) catch &buffer; | |
| 70 | 75 | return formatIdent(buf, "", .{}, writer); |
| 71 | 76 | } |
| 72 | 77 | |
| 73 | pub fn typeToCIdentifier(t: Type) std.fmt.Formatter(formatTypeAsCIdentifier) { | |
| 74 | return .{ .data = t }; | |
| 78 | pub fn typeToCIdentifier(ty: Type, target: std.Target) std.fmt.Formatter(formatTypeAsCIdentifier) { | |
| 79 | return .{ .data = .{ | |
| 80 | .ty = ty, | |
| 81 | .target = target, | |
| 82 | } }; | |
| 75 | 83 | } |
| 76 | 84 | |
| 77 | 85 | const reserved_idents = std.ComptimeStringMap(void, .{ |
| ... | ... | @@ -369,6 +377,8 @@ pub const DeclGen = struct { |
| 369 | 377 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 370 | 378 | decl.markAlive(); |
| 371 | 379 | |
| 380 | const target = dg.module.getTarget(); | |
| 381 | ||
| 372 | 382 | if (ty.isSlice()) { |
| 373 | 383 | try writer.writeByte('('); |
| 374 | 384 | try dg.renderTypecast(writer, ty); |
| ... | ... | @@ -376,7 +386,7 @@ pub const DeclGen = struct { |
| 376 | 386 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 377 | 387 | try dg.renderValue(writer, ty.slicePtrFieldType(&buf), val.slicePtr()); |
| 378 | 388 | try writer.writeAll(", "); |
| 379 | try writer.print("{d}", .{val.sliceLen()}); | |
| 389 | try writer.print("{d}", .{val.sliceLen(target)}); | |
| 380 | 390 | try writer.writeAll("}"); |
| 381 | 391 | return; |
| 382 | 392 | } |
| ... | ... | @@ -388,7 +398,7 @@ pub const DeclGen = struct { |
| 388 | 398 | // somewhere and we should let the C compiler tell us about it. |
| 389 | 399 | if (ty.castPtrToFn() == null) { |
| 390 | 400 | // Determine if we must pointer cast. |
| 391 | if (ty.eql(decl.ty)) { | |
| 401 | if (ty.eql(decl.ty, target)) { | |
| 392 | 402 | try writer.writeByte('&'); |
| 393 | 403 | try dg.renderDeclName(writer, decl); |
| 394 | 404 | return; |
| ... | ... | @@ -508,6 +518,7 @@ pub const DeclGen = struct { |
| 508 | 518 | ty: Type, |
| 509 | 519 | val: Value, |
| 510 | 520 | ) error{ OutOfMemory, AnalysisFail }!void { |
| 521 | const target = dg.module.getTarget(); | |
| 511 | 522 | if (val.isUndefDeep()) { |
| 512 | 523 | switch (ty.zigTypeTag()) { |
| 513 | 524 | // Using '{}' for integer and floats seemed to error C compilers (both GCC and Clang) |
| ... | ... | @@ -551,7 +562,7 @@ pub const DeclGen = struct { |
| 551 | 562 | else => { |
| 552 | 563 | if (ty.isSignedInt()) |
| 553 | 564 | return writer.print("{d}", .{val.toSignedInt()}); |
| 554 | return writer.print("{d}u", .{val.toUnsignedInt()}); | |
| 565 | return writer.print("{d}u", .{val.toUnsignedInt(target)}); | |
| 555 | 566 | }, |
| 556 | 567 | }, |
| 557 | 568 | .Float => { |
| ... | ... | @@ -609,7 +620,7 @@ pub const DeclGen = struct { |
| 609 | 620 | .int_u64, .one => { |
| 610 | 621 | try writer.writeAll("(("); |
| 611 | 622 | try dg.renderTypecast(writer, ty); |
| 612 | try writer.print(")0x{x}u)", .{val.toUnsignedInt()}); | |
| 623 | try writer.print(")0x{x}u)", .{val.toUnsignedInt(target)}); | |
| 613 | 624 | }, |
| 614 | 625 | else => unreachable, |
| 615 | 626 | }, |
| ... | ... | @@ -653,7 +664,6 @@ pub const DeclGen = struct { |
| 653 | 664 | if (ty.isPtrLikeOptional()) { |
| 654 | 665 | return dg.renderValue(writer, payload_type, val); |
| 655 | 666 | } |
| 656 | const target = dg.module.getTarget(); | |
| 657 | 667 | if (payload_type.abiSize(target) == 0) { |
| 658 | 668 | const is_null = val.castTag(.opt_payload) == null; |
| 659 | 669 | return writer.print("{}", .{is_null}); |
| ... | ... | @@ -773,7 +783,6 @@ pub const DeclGen = struct { |
| 773 | 783 | .Union => { |
| 774 | 784 | const union_obj = val.castTag(.@"union").?.data; |
| 775 | 785 | const union_ty = ty.cast(Type.Payload.Union).?.data; |
| 776 | const target = dg.module.getTarget(); | |
| 777 | 786 | const layout = ty.unionGetLayout(target); |
| 778 | 787 | |
| 779 | 788 | try writer.writeAll("("); |
| ... | ... | @@ -789,7 +798,7 @@ pub const DeclGen = struct { |
| 789 | 798 | try writer.writeAll(".payload = {"); |
| 790 | 799 | } |
| 791 | 800 | |
| 792 | const index = union_ty.tag_ty.enumTagFieldIndex(union_obj.tag).?; | |
| 801 | const index = union_ty.tag_ty.enumTagFieldIndex(union_obj.tag, target).?; | |
| 793 | 802 | const field_ty = ty.unionFields().values()[index].ty; |
| 794 | 803 | const field_name = ty.unionFields().keys()[index]; |
| 795 | 804 | if (field_ty.hasRuntimeBits()) { |
| ... | ... | @@ -879,8 +888,8 @@ pub const DeclGen = struct { |
| 879 | 888 | try bw.writeAll(" (*"); |
| 880 | 889 | |
| 881 | 890 | const name_start = buffer.items.len; |
| 882 | // TODO: typeToCIdentifier truncates to 128 bytes, we probably don't want to do this | |
| 883 | try bw.print("zig_F_{s})(", .{typeToCIdentifier(t)}); | |
| 891 | const target = dg.module.getTarget(); | |
| 892 | try bw.print("zig_F_{s})(", .{typeToCIdentifier(t, target)}); | |
| 884 | 893 | const name_end = buffer.items.len - 2; |
| 885 | 894 | |
| 886 | 895 | const param_len = fn_info.param_types.len; |
| ... | ... | @@ -934,10 +943,11 @@ pub const DeclGen = struct { |
| 934 | 943 | |
| 935 | 944 | try bw.writeAll("; size_t len; } "); |
| 936 | 945 | const name_index = buffer.items.len; |
| 946 | const target = dg.module.getTarget(); | |
| 937 | 947 | if (t.isConstPtr()) { |
| 938 | try bw.print("zig_L_{s}", .{typeToCIdentifier(child_type)}); | |
| 948 | try bw.print("zig_L_{s}", .{typeToCIdentifier(child_type, target)}); | |
| 939 | 949 | } else { |
| 940 | try bw.print("zig_M_{s}", .{typeToCIdentifier(child_type)}); | |
| 950 | try bw.print("zig_M_{s}", .{typeToCIdentifier(child_type, target)}); | |
| 941 | 951 | } |
| 942 | 952 | if (ptr_sentinel) |s| { |
| 943 | 953 | try bw.writeAll("_s_"); |
| ... | ... | @@ -1023,7 +1033,8 @@ pub const DeclGen = struct { |
| 1023 | 1033 | try buffer.appendSlice("} "); |
| 1024 | 1034 | |
| 1025 | 1035 | const name_start = buffer.items.len; |
| 1026 | try writer.print("zig_T_{};\n", .{typeToCIdentifier(t)}); | |
| 1036 | const target = dg.module.getTarget(); | |
| 1037 | try writer.print("zig_T_{};\n", .{typeToCIdentifier(t, target)}); | |
| 1027 | 1038 | |
| 1028 | 1039 | const rendered = buffer.toOwnedSlice(); |
| 1029 | 1040 | errdefer dg.typedefs.allocator.free(rendered); |
| ... | ... | @@ -1107,6 +1118,7 @@ pub const DeclGen = struct { |
| 1107 | 1118 | try dg.renderTypeAndName(bw, child_type, payload_name, .Mut, 0); |
| 1108 | 1119 | try bw.writeAll("; uint16_t error; } "); |
| 1109 | 1120 | const name_index = buffer.items.len; |
| 1121 | const target = dg.module.getTarget(); | |
| 1110 | 1122 | if (err_set_type.castTag(.error_set_inferred)) |inf_err_set_payload| { |
| 1111 | 1123 | const func = inf_err_set_payload.data.func; |
| 1112 | 1124 | try bw.writeAll("zig_E_"); |
| ... | ... | @@ -1114,7 +1126,7 @@ pub const DeclGen = struct { |
| 1114 | 1126 | try bw.writeAll(";\n"); |
| 1115 | 1127 | } else { |
| 1116 | 1128 | try bw.print("zig_E_{s}_{s};\n", .{ |
| 1117 | typeToCIdentifier(err_set_type), typeToCIdentifier(child_type), | |
| 1129 | typeToCIdentifier(err_set_type, target), typeToCIdentifier(child_type, target), | |
| 1118 | 1130 | }); |
| 1119 | 1131 | } |
| 1120 | 1132 | |
| ... | ... | @@ -1144,7 +1156,8 @@ pub const DeclGen = struct { |
| 1144 | 1156 | try dg.renderType(bw, elem_type); |
| 1145 | 1157 | |
| 1146 | 1158 | const name_start = buffer.items.len + 1; |
| 1147 | try bw.print(" zig_A_{s}_{d}", .{ typeToCIdentifier(elem_type), c_len }); | |
| 1159 | const target = dg.module.getTarget(); | |
| 1160 | try bw.print(" zig_A_{s}_{d}", .{ typeToCIdentifier(elem_type, target), c_len }); | |
| 1148 | 1161 | const name_end = buffer.items.len; |
| 1149 | 1162 | |
| 1150 | 1163 | try bw.print("[{d}];\n", .{c_len}); |
| ... | ... | @@ -1172,7 +1185,8 @@ pub const DeclGen = struct { |
| 1172 | 1185 | try dg.renderTypeAndName(bw, child_type, payload_name, .Mut, 0); |
| 1173 | 1186 | try bw.writeAll("; bool is_null; } "); |
| 1174 | 1187 | const name_index = buffer.items.len; |
| 1175 | try bw.print("zig_Q_{s};\n", .{typeToCIdentifier(child_type)}); | |
| 1188 | const target = dg.module.getTarget(); | |
| 1189 | try bw.print("zig_Q_{s};\n", .{typeToCIdentifier(child_type, target)}); | |
| 1176 | 1190 | |
| 1177 | 1191 | const rendered = buffer.toOwnedSlice(); |
| 1178 | 1192 | errdefer dg.typedefs.allocator.free(rendered); |
| ... | ... | @@ -2177,12 +2191,13 @@ fn airStore(f: *Function, inst: Air.Inst.Index) !CValue { |
| 2177 | 2191 | if (src_val_is_undefined) |
| 2178 | 2192 | return try airStoreUndefined(f, dest_ptr); |
| 2179 | 2193 | |
| 2194 | const target = f.object.dg.module.getTarget(); | |
| 2180 | 2195 | const writer = f.object.writer(); |
| 2181 | 2196 | if (lhs_child_type.zigTypeTag() == .Array) { |
| 2182 | 2197 | // For this memcpy to safely work we need the rhs to have the same |
| 2183 | 2198 | // underlying type as the lhs (i.e. they must both be arrays of the same underlying type). |
| 2184 | 2199 | const rhs_type = f.air.typeOf(bin_op.rhs); |
| 2185 | assert(rhs_type.eql(lhs_child_type)); | |
| 2200 | assert(rhs_type.eql(lhs_child_type, target)); | |
| 2186 | 2201 | |
| 2187 | 2202 | // If the source is a constant, writeCValue will emit a brace initialization |
| 2188 | 2203 | // so work around this by initializing into new local. |
src/codegen/llvm.zig+54-52| ... | ... | @@ -812,7 +812,7 @@ pub const Object = struct { |
| 812 | 812 | const gpa = o.gpa; |
| 813 | 813 | // Be careful not to reference this `gop` variable after any recursive calls |
| 814 | 814 | // to `lowerDebugType`. |
| 815 | const gop = try o.di_type_map.getOrPut(gpa, ty); | |
| 815 | const gop = try o.di_type_map.getOrPutContext(gpa, ty, .{ .target = o.target }); | |
| 816 | 816 | if (gop.found_existing) { |
| 817 | 817 | const annotated = gop.value_ptr.*; |
| 818 | 818 | const di_type = annotated.toDIType(); |
| ... | ... | @@ -825,7 +825,7 @@ pub const Object = struct { |
| 825 | 825 | }; |
| 826 | 826 | return o.lowerDebugTypeImpl(entry, resolve, di_type); |
| 827 | 827 | } |
| 828 | errdefer assert(o.di_type_map.orderedRemove(ty)); | |
| 828 | errdefer assert(o.di_type_map.orderedRemoveContext(ty, .{ .target = o.target })); | |
| 829 | 829 | // The Type memory is ephemeral; since we want to store a longer-lived |
| 830 | 830 | // reference, we need to copy it here. |
| 831 | 831 | gop.key_ptr.* = try ty.copy(o.type_map_arena.allocator()); |
| ... | ... | @@ -856,7 +856,7 @@ pub const Object = struct { |
| 856 | 856 | .Int => { |
| 857 | 857 | const info = ty.intInfo(target); |
| 858 | 858 | assert(info.bits != 0); |
| 859 | const name = try ty.nameAlloc(gpa); | |
| 859 | const name = try ty.nameAlloc(gpa, target); | |
| 860 | 860 | defer gpa.free(name); |
| 861 | 861 | const dwarf_encoding: c_uint = switch (info.signedness) { |
| 862 | 862 | .signed => DW.ATE.signed, |
| ... | ... | @@ -873,7 +873,7 @@ pub const Object = struct { |
| 873 | 873 | const enum_di_ty = try o.makeEmptyNamespaceDIType(owner_decl); |
| 874 | 874 | // The recursive call to `lowerDebugType` via `makeEmptyNamespaceDIType` |
| 875 | 875 | // means we can't use `gop` anymore. |
| 876 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(enum_di_ty)); | |
| 876 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(enum_di_ty), .{ .target = o.target }); | |
| 877 | 877 | return enum_di_ty; |
| 878 | 878 | } |
| 879 | 879 | |
| ... | ... | @@ -903,7 +903,7 @@ pub const Object = struct { |
| 903 | 903 | const di_file = try o.getDIFile(gpa, owner_decl.src_namespace.file_scope); |
| 904 | 904 | const di_scope = try o.namespaceToDebugScope(owner_decl.src_namespace); |
| 905 | 905 | |
| 906 | const name = try ty.nameAlloc(gpa); | |
| 906 | const name = try ty.nameAlloc(gpa, target); | |
| 907 | 907 | defer gpa.free(name); |
| 908 | 908 | var buffer: Type.Payload.Bits = undefined; |
| 909 | 909 | const int_ty = ty.intTagType(&buffer); |
| ... | ... | @@ -921,12 +921,12 @@ pub const Object = struct { |
| 921 | 921 | "", |
| 922 | 922 | ); |
| 923 | 923 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 924 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(enum_di_ty)); | |
| 924 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(enum_di_ty), .{ .target = o.target }); | |
| 925 | 925 | return enum_di_ty; |
| 926 | 926 | }, |
| 927 | 927 | .Float => { |
| 928 | 928 | const bits = ty.floatBits(target); |
| 929 | const name = try ty.nameAlloc(gpa); | |
| 929 | const name = try ty.nameAlloc(gpa, target); | |
| 930 | 930 | defer gpa.free(name); |
| 931 | 931 | const di_type = dib.createBasicType(name, bits, DW.ATE.float); |
| 932 | 932 | gop.value_ptr.* = AnnotatedDITypePtr.initFull(di_type); |
| ... | ... | @@ -974,7 +974,7 @@ pub const Object = struct { |
| 974 | 974 | const bland_ptr_ty = Type.initPayload(&payload.base); |
| 975 | 975 | const ptr_di_ty = try o.lowerDebugType(bland_ptr_ty, resolve); |
| 976 | 976 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 977 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.init(ptr_di_ty, resolve)); | |
| 977 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.init(ptr_di_ty, resolve), .{ .target = o.target }); | |
| 978 | 978 | return ptr_di_ty; |
| 979 | 979 | } |
| 980 | 980 | |
| ... | ... | @@ -983,7 +983,7 @@ pub const Object = struct { |
| 983 | 983 | const ptr_ty = ty.slicePtrFieldType(&buf); |
| 984 | 984 | const len_ty = Type.usize; |
| 985 | 985 | |
| 986 | const name = try ty.nameAlloc(gpa); | |
| 986 | const name = try ty.nameAlloc(gpa, target); | |
| 987 | 987 | defer gpa.free(name); |
| 988 | 988 | const di_file: ?*llvm.DIFile = null; |
| 989 | 989 | const line = 0; |
| ... | ... | @@ -1054,12 +1054,12 @@ pub const Object = struct { |
| 1054 | 1054 | ); |
| 1055 | 1055 | dib.replaceTemporary(fwd_decl, full_di_ty); |
| 1056 | 1056 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1057 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(full_di_ty)); | |
| 1057 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(full_di_ty), .{ .target = o.target }); | |
| 1058 | 1058 | return full_di_ty; |
| 1059 | 1059 | } |
| 1060 | 1060 | |
| 1061 | 1061 | const elem_di_ty = try o.lowerDebugType(ptr_info.pointee_type, .fwd); |
| 1062 | const name = try ty.nameAlloc(gpa); | |
| 1062 | const name = try ty.nameAlloc(gpa, target); | |
| 1063 | 1063 | defer gpa.free(name); |
| 1064 | 1064 | const ptr_di_ty = dib.createPointerType( |
| 1065 | 1065 | elem_di_ty, |
| ... | ... | @@ -1068,7 +1068,7 @@ pub const Object = struct { |
| 1068 | 1068 | name, |
| 1069 | 1069 | ); |
| 1070 | 1070 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1071 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(ptr_di_ty)); | |
| 1071 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(ptr_di_ty), .{ .target = o.target }); | |
| 1072 | 1072 | return ptr_di_ty; |
| 1073 | 1073 | }, |
| 1074 | 1074 | .Opaque => { |
| ... | ... | @@ -1077,7 +1077,7 @@ pub const Object = struct { |
| 1077 | 1077 | gop.value_ptr.* = AnnotatedDITypePtr.initFull(di_ty); |
| 1078 | 1078 | return di_ty; |
| 1079 | 1079 | } |
| 1080 | const name = try ty.nameAlloc(gpa); | |
| 1080 | const name = try ty.nameAlloc(gpa, target); | |
| 1081 | 1081 | defer gpa.free(name); |
| 1082 | 1082 | const owner_decl = ty.getOwnerDecl(); |
| 1083 | 1083 | const opaque_di_ty = dib.createForwardDeclType( |
| ... | ... | @@ -1089,7 +1089,7 @@ pub const Object = struct { |
| 1089 | 1089 | ); |
| 1090 | 1090 | // The recursive call to `lowerDebugType` va `namespaceToDebugScope` |
| 1091 | 1091 | // means we can't use `gop` anymore. |
| 1092 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(opaque_di_ty)); | |
| 1092 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(opaque_di_ty), .{ .target = o.target }); | |
| 1093 | 1093 | return opaque_di_ty; |
| 1094 | 1094 | }, |
| 1095 | 1095 | .Array => { |
| ... | ... | @@ -1100,7 +1100,7 @@ pub const Object = struct { |
| 1100 | 1100 | @intCast(c_int, ty.arrayLen()), |
| 1101 | 1101 | ); |
| 1102 | 1102 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1103 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(array_di_ty)); | |
| 1103 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(array_di_ty), .{ .target = o.target }); | |
| 1104 | 1104 | return array_di_ty; |
| 1105 | 1105 | }, |
| 1106 | 1106 | .Vector => { |
| ... | ... | @@ -1111,11 +1111,11 @@ pub const Object = struct { |
| 1111 | 1111 | ty.vectorLen(), |
| 1112 | 1112 | ); |
| 1113 | 1113 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1114 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(vector_di_ty)); | |
| 1114 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(vector_di_ty), .{ .target = o.target }); | |
| 1115 | 1115 | return vector_di_ty; |
| 1116 | 1116 | }, |
| 1117 | 1117 | .Optional => { |
| 1118 | const name = try ty.nameAlloc(gpa); | |
| 1118 | const name = try ty.nameAlloc(gpa, target); | |
| 1119 | 1119 | defer gpa.free(name); |
| 1120 | 1120 | var buf: Type.Payload.ElemType = undefined; |
| 1121 | 1121 | const child_ty = ty.optionalChild(&buf); |
| ... | ... | @@ -1127,7 +1127,7 @@ pub const Object = struct { |
| 1127 | 1127 | if (ty.isPtrLikeOptional()) { |
| 1128 | 1128 | const ptr_di_ty = try o.lowerDebugType(child_ty, resolve); |
| 1129 | 1129 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1130 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(ptr_di_ty)); | |
| 1130 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(ptr_di_ty), .{ .target = o.target }); | |
| 1131 | 1131 | return ptr_di_ty; |
| 1132 | 1132 | } |
| 1133 | 1133 | |
| ... | ... | @@ -1200,7 +1200,7 @@ pub const Object = struct { |
| 1200 | 1200 | ); |
| 1201 | 1201 | dib.replaceTemporary(fwd_decl, full_di_ty); |
| 1202 | 1202 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1203 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(full_di_ty)); | |
| 1203 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(full_di_ty), .{ .target = o.target }); | |
| 1204 | 1204 | return full_di_ty; |
| 1205 | 1205 | }, |
| 1206 | 1206 | .ErrorUnion => { |
| ... | ... | @@ -1209,10 +1209,10 @@ pub const Object = struct { |
| 1209 | 1209 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 1210 | 1210 | const err_set_di_ty = try o.lowerDebugType(err_set_ty, .full); |
| 1211 | 1211 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1212 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(err_set_di_ty)); | |
| 1212 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(err_set_di_ty), .{ .target = o.target }); | |
| 1213 | 1213 | return err_set_di_ty; |
| 1214 | 1214 | } |
| 1215 | const name = try ty.nameAlloc(gpa); | |
| 1215 | const name = try ty.nameAlloc(gpa, target); | |
| 1216 | 1216 | defer gpa.free(name); |
| 1217 | 1217 | const di_file: ?*llvm.DIFile = null; |
| 1218 | 1218 | const line = 0; |
| ... | ... | @@ -1282,7 +1282,7 @@ pub const Object = struct { |
| 1282 | 1282 | ); |
| 1283 | 1283 | dib.replaceTemporary(fwd_decl, full_di_ty); |
| 1284 | 1284 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1285 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(full_di_ty)); | |
| 1285 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(full_di_ty), .{ .target = o.target }); | |
| 1286 | 1286 | return full_di_ty; |
| 1287 | 1287 | }, |
| 1288 | 1288 | .ErrorSet => { |
| ... | ... | @@ -1294,7 +1294,7 @@ pub const Object = struct { |
| 1294 | 1294 | }, |
| 1295 | 1295 | .Struct => { |
| 1296 | 1296 | const compile_unit_scope = o.di_compile_unit.?.toScope(); |
| 1297 | const name = try ty.nameAlloc(gpa); | |
| 1297 | const name = try ty.nameAlloc(gpa, target); | |
| 1298 | 1298 | defer gpa.free(name); |
| 1299 | 1299 | |
| 1300 | 1300 | if (ty.castTag(.@"struct")) |payload| { |
| ... | ... | @@ -1381,7 +1381,7 @@ pub const Object = struct { |
| 1381 | 1381 | ); |
| 1382 | 1382 | dib.replaceTemporary(fwd_decl, full_di_ty); |
| 1383 | 1383 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1384 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(full_di_ty)); | |
| 1384 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(full_di_ty), .{ .target = o.target }); | |
| 1385 | 1385 | return full_di_ty; |
| 1386 | 1386 | } |
| 1387 | 1387 | |
| ... | ... | @@ -1395,7 +1395,7 @@ pub const Object = struct { |
| 1395 | 1395 | dib.replaceTemporary(fwd_decl, struct_di_ty); |
| 1396 | 1396 | // The recursive call to `lowerDebugType` via `makeEmptyNamespaceDIType` |
| 1397 | 1397 | // means we can't use `gop` anymore. |
| 1398 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(struct_di_ty)); | |
| 1398 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(struct_di_ty), .{ .target = o.target }); | |
| 1399 | 1399 | return struct_di_ty; |
| 1400 | 1400 | } |
| 1401 | 1401 | } |
| ... | ... | @@ -1406,7 +1406,7 @@ pub const Object = struct { |
| 1406 | 1406 | dib.replaceTemporary(fwd_decl, struct_di_ty); |
| 1407 | 1407 | // The recursive call to `lowerDebugType` via `makeEmptyNamespaceDIType` |
| 1408 | 1408 | // means we can't use `gop` anymore. |
| 1409 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(struct_di_ty)); | |
| 1409 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(struct_di_ty), .{ .target = o.target }); | |
| 1410 | 1410 | return struct_di_ty; |
| 1411 | 1411 | } |
| 1412 | 1412 | |
| ... | ... | @@ -1461,13 +1461,13 @@ pub const Object = struct { |
| 1461 | 1461 | ); |
| 1462 | 1462 | dib.replaceTemporary(fwd_decl, full_di_ty); |
| 1463 | 1463 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1464 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(full_di_ty)); | |
| 1464 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(full_di_ty), .{ .target = o.target }); | |
| 1465 | 1465 | return full_di_ty; |
| 1466 | 1466 | }, |
| 1467 | 1467 | .Union => { |
| 1468 | 1468 | const owner_decl = ty.getOwnerDecl(); |
| 1469 | 1469 | |
| 1470 | const name = try ty.nameAlloc(gpa); | |
| 1470 | const name = try ty.nameAlloc(gpa, target); | |
| 1471 | 1471 | defer gpa.free(name); |
| 1472 | 1472 | |
| 1473 | 1473 | const fwd_decl = opt_fwd_decl orelse blk: { |
| ... | ... | @@ -1489,7 +1489,7 @@ pub const Object = struct { |
| 1489 | 1489 | dib.replaceTemporary(fwd_decl, union_di_ty); |
| 1490 | 1490 | // The recursive call to `lowerDebugType` via `makeEmptyNamespaceDIType` |
| 1491 | 1491 | // means we can't use `gop` anymore. |
| 1492 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(union_di_ty)); | |
| 1492 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(union_di_ty), .{ .target = o.target }); | |
| 1493 | 1493 | return union_di_ty; |
| 1494 | 1494 | } |
| 1495 | 1495 | |
| ... | ... | @@ -1603,7 +1603,7 @@ pub const Object = struct { |
| 1603 | 1603 | 0, |
| 1604 | 1604 | ); |
| 1605 | 1605 | // The recursive call to `lowerDebugType` means we can't use `gop` anymore. |
| 1606 | try o.di_type_map.put(gpa, ty, AnnotatedDITypePtr.initFull(fn_di_ty)); | |
| 1606 | try o.di_type_map.putContext(gpa, ty, AnnotatedDITypePtr.initFull(fn_di_ty), .{ .target = o.target }); | |
| 1607 | 1607 | return fn_di_ty; |
| 1608 | 1608 | }, |
| 1609 | 1609 | .ComptimeInt => unreachable, |
| ... | ... | @@ -1676,7 +1676,9 @@ pub const DeclGen = struct { |
| 1676 | 1676 | const decl = dg.decl; |
| 1677 | 1677 | assert(decl.has_tv); |
| 1678 | 1678 | |
| 1679 | log.debug("gen: {s} type: {}, value: {}", .{ decl.name, decl.ty, decl.val.fmtDebug() }); | |
| 1679 | log.debug("gen: {s} type: {}, value: {}", .{ | |
| 1680 | decl.name, decl.ty.fmtDebug(), decl.val.fmtDebug(), | |
| 1681 | }); | |
| 1680 | 1682 | |
| 1681 | 1683 | if (decl.val.castTag(.function)) |func_payload| { |
| 1682 | 1684 | _ = func_payload; |
| ... | ... | @@ -1990,7 +1992,7 @@ pub const DeclGen = struct { |
| 1990 | 1992 | }, |
| 1991 | 1993 | .Opaque => switch (t.tag()) { |
| 1992 | 1994 | .@"opaque" => { |
| 1993 | const gop = try dg.object.type_map.getOrPut(gpa, t); | |
| 1995 | const gop = try dg.object.type_map.getOrPutContext(gpa, t, .{ .target = target }); | |
| 1994 | 1996 | if (gop.found_existing) return gop.value_ptr.*; |
| 1995 | 1997 | |
| 1996 | 1998 | // The Type memory is ephemeral; since we want to store a longer-lived |
| ... | ... | @@ -2051,7 +2053,7 @@ pub const DeclGen = struct { |
| 2051 | 2053 | return dg.context.intType(16); |
| 2052 | 2054 | }, |
| 2053 | 2055 | .Struct => { |
| 2054 | const gop = try dg.object.type_map.getOrPut(gpa, t); | |
| 2056 | const gop = try dg.object.type_map.getOrPutContext(gpa, t, .{ .target = target }); | |
| 2055 | 2057 | if (gop.found_existing) return gop.value_ptr.*; |
| 2056 | 2058 | |
| 2057 | 2059 | // The Type memory is ephemeral; since we want to store a longer-lived |
| ... | ... | @@ -2174,7 +2176,7 @@ pub const DeclGen = struct { |
| 2174 | 2176 | return llvm_struct_ty; |
| 2175 | 2177 | }, |
| 2176 | 2178 | .Union => { |
| 2177 | const gop = try dg.object.type_map.getOrPut(gpa, t); | |
| 2179 | const gop = try dg.object.type_map.getOrPutContext(gpa, t, .{ .target = target }); | |
| 2178 | 2180 | if (gop.found_existing) return gop.value_ptr.*; |
| 2179 | 2181 | |
| 2180 | 2182 | // The Type memory is ephemeral; since we want to store a longer-lived |
| ... | ... | @@ -2289,6 +2291,7 @@ pub const DeclGen = struct { |
| 2289 | 2291 | const llvm_type = try dg.llvmType(tv.ty); |
| 2290 | 2292 | return llvm_type.getUndef(); |
| 2291 | 2293 | } |
| 2294 | const target = dg.module.getTarget(); | |
| 2292 | 2295 | |
| 2293 | 2296 | switch (tv.ty.zigTypeTag()) { |
| 2294 | 2297 | .Bool => { |
| ... | ... | @@ -2302,8 +2305,7 @@ pub const DeclGen = struct { |
| 2302 | 2305 | .decl_ref => return lowerDeclRefValue(dg, tv, tv.val.castTag(.decl_ref).?.data), |
| 2303 | 2306 | else => { |
| 2304 | 2307 | var bigint_space: Value.BigIntSpace = undefined; |
| 2305 | const bigint = tv.val.toBigInt(&bigint_space); | |
| 2306 | const target = dg.module.getTarget(); | |
| 2308 | const bigint = tv.val.toBigInt(&bigint_space, target); | |
| 2307 | 2309 | const int_info = tv.ty.intInfo(target); |
| 2308 | 2310 | assert(int_info.bits != 0); |
| 2309 | 2311 | const llvm_type = dg.context.intType(int_info.bits); |
| ... | ... | @@ -2331,9 +2333,8 @@ pub const DeclGen = struct { |
| 2331 | 2333 | const int_val = tv.enumToInt(&int_buffer); |
| 2332 | 2334 | |
| 2333 | 2335 | var bigint_space: Value.BigIntSpace = undefined; |
| 2334 | const bigint = int_val.toBigInt(&bigint_space); | |
| 2336 | const bigint = int_val.toBigInt(&bigint_space, target); | |
| 2335 | 2337 | |
| 2336 | const target = dg.module.getTarget(); | |
| 2337 | 2338 | const int_info = tv.ty.intInfo(target); |
| 2338 | 2339 | const llvm_type = dg.context.intType(int_info.bits); |
| 2339 | 2340 | |
| ... | ... | @@ -2356,7 +2357,6 @@ pub const DeclGen = struct { |
| 2356 | 2357 | }, |
| 2357 | 2358 | .Float => { |
| 2358 | 2359 | const llvm_ty = try dg.llvmType(tv.ty); |
| 2359 | const target = dg.module.getTarget(); | |
| 2360 | 2360 | switch (tv.ty.floatBits(target)) { |
| 2361 | 2361 | 16, 32, 64 => return llvm_ty.constReal(tv.val.toFloat(f64)), |
| 2362 | 2362 | 80 => { |
| ... | ... | @@ -2414,7 +2414,7 @@ pub const DeclGen = struct { |
| 2414 | 2414 | }, |
| 2415 | 2415 | .int_u64, .one, .int_big_positive => { |
| 2416 | 2416 | const llvm_usize = try dg.llvmType(Type.usize); |
| 2417 | const llvm_int = llvm_usize.constInt(tv.val.toUnsignedInt(), .False); | |
| 2417 | const llvm_int = llvm_usize.constInt(tv.val.toUnsignedInt(target), .False); | |
| 2418 | 2418 | return llvm_int.constIntToPtr(try dg.llvmType(tv.ty)); |
| 2419 | 2419 | }, |
| 2420 | 2420 | .field_ptr, .opt_payload_ptr, .eu_payload_ptr, .elem_ptr => { |
| ... | ... | @@ -2424,7 +2424,9 @@ pub const DeclGen = struct { |
| 2424 | 2424 | const llvm_type = try dg.llvmType(tv.ty); |
| 2425 | 2425 | return llvm_type.constNull(); |
| 2426 | 2426 | }, |
| 2427 | else => |tag| return dg.todo("implement const of pointer type '{}' ({})", .{ tv.ty, tag }), | |
| 2427 | else => |tag| return dg.todo("implement const of pointer type '{}' ({})", .{ | |
| 2428 | tv.ty.fmtDebug(), tag, | |
| 2429 | }), | |
| 2428 | 2430 | }, |
| 2429 | 2431 | .Array => switch (tv.val.tag()) { |
| 2430 | 2432 | .bytes => { |
| ... | ... | @@ -2592,7 +2594,6 @@ pub const DeclGen = struct { |
| 2592 | 2594 | const llvm_struct_ty = try dg.llvmType(tv.ty); |
| 2593 | 2595 | const field_vals = tv.val.castTag(.aggregate).?.data; |
| 2594 | 2596 | const gpa = dg.gpa; |
| 2595 | const target = dg.module.getTarget(); | |
| 2596 | 2597 | |
| 2597 | 2598 | if (tv.ty.isTupleOrAnonStruct()) { |
| 2598 | 2599 | const tuple = tv.ty.tupleFields(); |
| ... | ... | @@ -2753,7 +2754,6 @@ pub const DeclGen = struct { |
| 2753 | 2754 | const llvm_union_ty = try dg.llvmType(tv.ty); |
| 2754 | 2755 | const tag_and_val = tv.val.castTag(.@"union").?.data; |
| 2755 | 2756 | |
| 2756 | const target = dg.module.getTarget(); | |
| 2757 | 2757 | const layout = tv.ty.unionGetLayout(target); |
| 2758 | 2758 | |
| 2759 | 2759 | if (layout.payload_size == 0) { |
| ... | ... | @@ -2763,7 +2763,7 @@ pub const DeclGen = struct { |
| 2763 | 2763 | }); |
| 2764 | 2764 | } |
| 2765 | 2765 | const union_obj = tv.ty.cast(Type.Payload.Union).?.data; |
| 2766 | const field_index = union_obj.tag_ty.enumTagFieldIndex(tag_and_val.tag).?; | |
| 2766 | const field_index = union_obj.tag_ty.enumTagFieldIndex(tag_and_val.tag, target).?; | |
| 2767 | 2767 | assert(union_obj.haveFieldTypes()); |
| 2768 | 2768 | const field_ty = union_obj.fields.values()[field_index].ty; |
| 2769 | 2769 | const payload = p: { |
| ... | ... | @@ -2892,7 +2892,7 @@ pub const DeclGen = struct { |
| 2892 | 2892 | |
| 2893 | 2893 | .Frame, |
| 2894 | 2894 | .AnyFrame, |
| 2895 | => return dg.todo("implement const of type '{}'", .{tv.ty}), | |
| 2895 | => return dg.todo("implement const of type '{}'", .{tv.ty.fmtDebug()}), | |
| 2896 | 2896 | } |
| 2897 | 2897 | } |
| 2898 | 2898 | |
| ... | ... | @@ -2910,7 +2910,8 @@ pub const DeclGen = struct { |
| 2910 | 2910 | const ptr_ty = Type.initPayload(&ptr_ty_payload.base); |
| 2911 | 2911 | const llvm_ptr = try dg.lowerDeclRefValue(.{ .ty = ptr_ty, .val = ptr_val }, decl); |
| 2912 | 2912 | |
| 2913 | if (ptr_child_ty.eql(decl.ty)) { | |
| 2913 | const target = dg.module.getTarget(); | |
| 2914 | if (ptr_child_ty.eql(decl.ty, target)) { | |
| 2914 | 2915 | return llvm_ptr; |
| 2915 | 2916 | } else { |
| 2916 | 2917 | return llvm_ptr.constBitCast((try dg.llvmType(ptr_child_ty)).pointerType(0)); |
| ... | ... | @@ -2918,6 +2919,7 @@ pub const DeclGen = struct { |
| 2918 | 2919 | } |
| 2919 | 2920 | |
| 2920 | 2921 | fn lowerParentPtr(dg: *DeclGen, ptr_val: Value, ptr_child_ty: Type) Error!*const llvm.Value { |
| 2922 | const target = dg.module.getTarget(); | |
| 2921 | 2923 | var bitcast_needed: bool = undefined; |
| 2922 | 2924 | const llvm_ptr = switch (ptr_val.tag()) { |
| 2923 | 2925 | .decl_ref_mut => { |
| ... | ... | @@ -2951,7 +2953,6 @@ pub const DeclGen = struct { |
| 2951 | 2953 | |
| 2952 | 2954 | const field_index = @intCast(u32, field_ptr.field_index); |
| 2953 | 2955 | const llvm_u32 = dg.context.intType(32); |
| 2954 | const target = dg.module.getTarget(); | |
| 2955 | 2956 | switch (parent_ty.zigTypeTag()) { |
| 2956 | 2957 | .Union => { |
| 2957 | 2958 | bitcast_needed = true; |
| ... | ... | @@ -2974,7 +2975,7 @@ pub const DeclGen = struct { |
| 2974 | 2975 | }, |
| 2975 | 2976 | .Struct => { |
| 2976 | 2977 | const field_ty = parent_ty.structFieldType(field_index); |
| 2977 | bitcast_needed = !field_ty.eql(ptr_child_ty); | |
| 2978 | bitcast_needed = !field_ty.eql(ptr_child_ty, target); | |
| 2978 | 2979 | |
| 2979 | 2980 | var ty_buf: Type.Payload.Pointer = undefined; |
| 2980 | 2981 | const llvm_field_index = llvmFieldIndex(parent_ty, field_index, target, &ty_buf).?; |
| ... | ... | @@ -2990,7 +2991,7 @@ pub const DeclGen = struct { |
| 2990 | 2991 | .elem_ptr => blk: { |
| 2991 | 2992 | const elem_ptr = ptr_val.castTag(.elem_ptr).?.data; |
| 2992 | 2993 | const parent_llvm_ptr = try dg.lowerParentPtr(elem_ptr.array_ptr, elem_ptr.elem_ty); |
| 2993 | bitcast_needed = !elem_ptr.elem_ty.eql(ptr_child_ty); | |
| 2994 | bitcast_needed = !elem_ptr.elem_ty.eql(ptr_child_ty, target); | |
| 2994 | 2995 | |
| 2995 | 2996 | const llvm_usize = try dg.llvmType(Type.usize); |
| 2996 | 2997 | const indices: [1]*const llvm.Value = .{ |
| ... | ... | @@ -3004,7 +3005,7 @@ pub const DeclGen = struct { |
| 3004 | 3005 | var buf: Type.Payload.ElemType = undefined; |
| 3005 | 3006 | |
| 3006 | 3007 | const payload_ty = opt_payload_ptr.container_ty.optionalChild(&buf); |
| 3007 | bitcast_needed = !payload_ty.eql(ptr_child_ty); | |
| 3008 | bitcast_needed = !payload_ty.eql(ptr_child_ty, target); | |
| 3008 | 3009 | |
| 3009 | 3010 | if (!payload_ty.hasRuntimeBitsIgnoreComptime() or payload_ty.isPtrLikeOptional()) { |
| 3010 | 3011 | // In this case, we represent pointer to optional the same as pointer |
| ... | ... | @@ -3024,7 +3025,7 @@ pub const DeclGen = struct { |
| 3024 | 3025 | const parent_llvm_ptr = try dg.lowerParentPtr(eu_payload_ptr.container_ptr, eu_payload_ptr.container_ty); |
| 3025 | 3026 | |
| 3026 | 3027 | const payload_ty = eu_payload_ptr.container_ty.errorUnionPayload(); |
| 3027 | bitcast_needed = !payload_ty.eql(ptr_child_ty); | |
| 3028 | bitcast_needed = !payload_ty.eql(ptr_child_ty, target); | |
| 3028 | 3029 | |
| 3029 | 3030 | if (!payload_ty.hasRuntimeBitsIgnoreComptime()) { |
| 3030 | 3031 | // In this case, we represent pointer to error union the same as pointer |
| ... | ... | @@ -3053,12 +3054,13 @@ pub const DeclGen = struct { |
| 3053 | 3054 | tv: TypedValue, |
| 3054 | 3055 | decl: *Module.Decl, |
| 3055 | 3056 | ) Error!*const llvm.Value { |
| 3057 | const target = self.module.getTarget(); | |
| 3056 | 3058 | if (tv.ty.isSlice()) { |
| 3057 | 3059 | var buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 3058 | 3060 | const ptr_ty = tv.ty.slicePtrFieldType(&buf); |
| 3059 | 3061 | var slice_len: Value.Payload.U64 = .{ |
| 3060 | 3062 | .base = .{ .tag = .int_u64 }, |
| 3061 | .data = tv.val.sliceLen(), | |
| 3063 | .data = tv.val.sliceLen(target), | |
| 3062 | 3064 | }; |
| 3063 | 3065 | const fields: [2]*const llvm.Value = .{ |
| 3064 | 3066 | try self.genTypedValue(.{ |
src/codegen/spirv.zig+10-8| ... | ... | @@ -313,7 +313,7 @@ pub const DeclGen = struct { |
| 313 | 313 | // As of yet, there is no vector support in the self-hosted compiler. |
| 314 | 314 | .Vector => self.todo("implement arithmeticTypeInfo for Vector", .{}), |
| 315 | 315 | // TODO: For which types is this the case? |
| 316 | else => self.todo("implement arithmeticTypeInfo for {}", .{ty}), | |
| 316 | else => self.todo("implement arithmeticTypeInfo for {}", .{ty.fmtDebug()}), | |
| 317 | 317 | }; |
| 318 | 318 | } |
| 319 | 319 | |
| ... | ... | @@ -335,7 +335,7 @@ pub const DeclGen = struct { |
| 335 | 335 | const int_info = ty.intInfo(target); |
| 336 | 336 | const backing_bits = self.backingIntBits(int_info.bits) orelse { |
| 337 | 337 | // Integers too big for any native type are represented as "composite integers": An array of largestSupportedIntBits. |
| 338 | return self.todo("implement composite int constants for {}", .{ty}); | |
| 338 | return self.todo("implement composite int constants for {}", .{ty.fmtDebug()}); | |
| 339 | 339 | }; |
| 340 | 340 | |
| 341 | 341 | // We can just use toSignedInt/toUnsignedInt here as it returns u64 - a type large enough to hold any |
| ... | ... | @@ -345,7 +345,7 @@ pub const DeclGen = struct { |
| 345 | 345 | |
| 346 | 346 | // Note, value is required to be sign-extended, so we don't need to mask off the upper bits. |
| 347 | 347 | // See https://www.khronos.org/registry/SPIR-V/specs/unified1/SPIRV.html#Literal |
| 348 | var int_bits = if (ty.isSignedInt()) @bitCast(u64, val.toSignedInt()) else val.toUnsignedInt(); | |
| 348 | var int_bits = if (ty.isSignedInt()) @bitCast(u64, val.toSignedInt()) else val.toUnsignedInt(target); | |
| 349 | 349 | |
| 350 | 350 | const value: spec.LiteralContextDependentNumber = switch (backing_bits) { |
| 351 | 351 | 1...32 => .{ .uint32 = @truncate(u32, int_bits) }, |
| ... | ... | @@ -388,7 +388,7 @@ pub const DeclGen = struct { |
| 388 | 388 | }); |
| 389 | 389 | }, |
| 390 | 390 | .Void => unreachable, |
| 391 | else => return self.todo("constant generation of type {}", .{ty}), | |
| 391 | else => return self.todo("constant generation of type {}", .{ty.fmtDebug()}), | |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | 394 | return result_id.toRef(); |
| ... | ... | @@ -414,7 +414,7 @@ pub const DeclGen = struct { |
| 414 | 414 | const backing_bits = self.backingIntBits(int_info.bits) orelse { |
| 415 | 415 | // TODO: Integers too big for any native type are represented as "composite integers": |
| 416 | 416 | // An array of largestSupportedIntBits. |
| 417 | return self.todo("Implement composite int type {}", .{ty}); | |
| 417 | return self.todo("Implement composite int type {}", .{ty.fmtDebug()}); | |
| 418 | 418 | }; |
| 419 | 419 | |
| 420 | 420 | const payload = try self.spv.arena.create(SpvType.Payload.Int); |
| ... | ... | @@ -644,8 +644,10 @@ pub const DeclGen = struct { |
| 644 | 644 | const result_id = self.spv.allocId(); |
| 645 | 645 | const result_type_id = try self.resolveTypeId(ty); |
| 646 | 646 | |
| 647 | assert(self.air.typeOf(bin_op.lhs).eql(ty)); | |
| 648 | assert(self.air.typeOf(bin_op.rhs).eql(ty)); | |
| 647 | const target = self.getTarget(); | |
| 648 | ||
| 649 | assert(self.air.typeOf(bin_op.lhs).eql(ty, target)); | |
| 650 | assert(self.air.typeOf(bin_op.rhs).eql(ty, target)); | |
| 649 | 651 | |
| 650 | 652 | // Binary operations are generally applicable to both scalar and vector operations |
| 651 | 653 | // in SPIR-V, but int and float versions of operations require different opcodes. |
| ... | ... | @@ -692,7 +694,7 @@ pub const DeclGen = struct { |
| 692 | 694 | const result_id = self.spv.allocId(); |
| 693 | 695 | const result_type_id = try self.resolveTypeId(Type.initTag(.bool)); |
| 694 | 696 | const op_ty = self.air.typeOf(bin_op.lhs); |
| 695 | assert(op_ty.eql(self.air.typeOf(bin_op.rhs))); | |
| 697 | assert(op_ty.eql(self.air.typeOf(bin_op.rhs), self.getTarget())); | |
| 696 | 698 | |
| 697 | 699 | // Comparisons are generally applicable to both scalar and vector operations in SPIR-V, |
| 698 | 700 | // but int and float versions of operations require different opcodes. |
src/link.zig+2-2| ... | ... | @@ -457,7 +457,7 @@ pub const File = struct { |
| 457 | 457 | /// May be called before or after updateDeclExports but must be called |
| 458 | 458 | /// after allocateDeclIndexes for any given Decl. |
| 459 | 459 | pub fn updateDecl(base: *File, module: *Module, decl: *Module.Decl) UpdateDeclError!void { |
| 460 | log.debug("updateDecl {*} ({s}), type={}", .{ decl, decl.name, decl.ty }); | |
| 460 | log.debug("updateDecl {*} ({s}), type={}", .{ decl, decl.name, decl.ty.fmtDebug() }); | |
| 461 | 461 | assert(decl.has_tv); |
| 462 | 462 | switch (base.tag) { |
| 463 | 463 | // zig fmt: off |
| ... | ... | @@ -477,7 +477,7 @@ pub const File = struct { |
| 477 | 477 | /// after allocateDeclIndexes for any given Decl. |
| 478 | 478 | pub fn updateFunc(base: *File, module: *Module, func: *Module.Fn, air: Air, liveness: Liveness) UpdateDeclError!void { |
| 479 | 479 | log.debug("updateFunc {*} ({s}), type={}", .{ |
| 480 | func.owner_decl, func.owner_decl.name, func.owner_decl.ty, | |
| 480 | func.owner_decl, func.owner_decl.name, func.owner_decl.ty.fmtDebug(), | |
| 481 | 481 | }); |
| 482 | 482 | switch (base.tag) { |
| 483 | 483 | // zig fmt: off |
src/link/C.zig+5-3| ... | ... | @@ -127,7 +127,7 @@ pub fn updateFunc(self: *C, module: *Module, func: *Module.Fn, air: Air, livenes |
| 127 | 127 | .error_msg = null, |
| 128 | 128 | .decl = decl, |
| 129 | 129 | .fwd_decl = fwd_decl.toManaged(module.gpa), |
| 130 | .typedefs = typedefs.promote(module.gpa), | |
| 130 | .typedefs = typedefs.promoteContext(module.gpa, .{ .target = module.getTarget() }), | |
| 131 | 131 | .typedefs_arena = self.arena.allocator(), |
| 132 | 132 | }, |
| 133 | 133 | .code = code.toManaged(module.gpa), |
| ... | ... | @@ -192,7 +192,7 @@ pub fn updateDecl(self: *C, module: *Module, decl: *Module.Decl) !void { |
| 192 | 192 | .error_msg = null, |
| 193 | 193 | .decl = decl, |
| 194 | 194 | .fwd_decl = fwd_decl.toManaged(module.gpa), |
| 195 | .typedefs = typedefs.promote(module.gpa), | |
| 195 | .typedefs = typedefs.promoteContext(module.gpa, .{ .target = module.getTarget() }), | |
| 196 | 196 | .typedefs_arena = self.arena.allocator(), |
| 197 | 197 | }, |
| 198 | 198 | .code = code.toManaged(module.gpa), |
| ... | ... | @@ -366,7 +366,9 @@ fn flushDecl(self: *C, f: *Flush, decl: *const Module.Decl) FlushDeclError!void |
| 366 | 366 | try f.typedefs.ensureUnusedCapacity(gpa, @intCast(u32, decl_block.typedefs.count())); |
| 367 | 367 | var it = decl_block.typedefs.iterator(); |
| 368 | 368 | while (it.next()) |new| { |
| 369 | const gop = f.typedefs.getOrPutAssumeCapacity(new.key_ptr.*); | |
| 369 | const gop = f.typedefs.getOrPutAssumeCapacityContext(new.key_ptr.*, .{ | |
| 370 | .target = self.base.options.target, | |
| 371 | }); | |
| 370 | 372 | if (!gop.found_existing) { |
| 371 | 373 | try f.err_typedef_buf.appendSlice(gpa, new.value_ptr.rendered); |
| 372 | 374 | } |
src/link/Dwarf.zig+15-9| ... | ... | @@ -200,7 +200,9 @@ pub fn initDeclDebugInfo(self: *Dwarf, decl: *Module.Decl) !DeclDebugBuffers { |
| 200 | 200 | assert(self.getRelocDbgInfoSubprogramHighPC() == dbg_info_buffer.items.len); |
| 201 | 201 | dbg_info_buffer.items.len += 4; // DW.AT.high_pc, DW.FORM.data4 |
| 202 | 202 | if (fn_ret_has_bits) { |
| 203 | const gop = try dbg_info_type_relocs.getOrPut(gpa, fn_ret_type); | |
| 203 | const gop = try dbg_info_type_relocs.getOrPutContext(gpa, fn_ret_type, .{ | |
| 204 | .target = self.target, | |
| 205 | }); | |
| 204 | 206 | if (!gop.found_existing) { |
| 205 | 207 | gop.value_ptr.* = .{ |
| 206 | 208 | .off = undefined, |
| ... | ... | @@ -455,7 +457,9 @@ pub fn commitDeclDebugInfo( |
| 455 | 457 | var it: usize = 0; |
| 456 | 458 | while (it < dbg_info_type_relocs.count()) : (it += 1) { |
| 457 | 459 | const ty = dbg_info_type_relocs.keys()[it]; |
| 458 | const value_ptr = dbg_info_type_relocs.getPtr(ty).?; | |
| 460 | const value_ptr = dbg_info_type_relocs.getPtrContext(ty, .{ | |
| 461 | .target = self.target, | |
| 462 | }).?; | |
| 459 | 463 | value_ptr.off = @intCast(u32, dbg_info_buffer.items.len); |
| 460 | 464 | try self.addDbgInfoType(dbg_type_arena.allocator(), ty, dbg_info_buffer, dbg_info_type_relocs); |
| 461 | 465 | } |
| ... | ... | @@ -774,7 +778,7 @@ fn addDbgInfoType( |
| 774 | 778 | // DW.AT.byte_size, DW.FORM.data1 |
| 775 | 779 | dbg_info_buffer.appendAssumeCapacity(@intCast(u8, ty.abiSize(target))); |
| 776 | 780 | // DW.AT.name, DW.FORM.string |
| 777 | try dbg_info_buffer.writer().print("{}\x00", .{ty}); | |
| 781 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(target)}); | |
| 778 | 782 | }, |
| 779 | 783 | .Optional => { |
| 780 | 784 | if (ty.isPtrLikeOptional()) { |
| ... | ... | @@ -785,7 +789,7 @@ fn addDbgInfoType( |
| 785 | 789 | // DW.AT.byte_size, DW.FORM.data1 |
| 786 | 790 | dbg_info_buffer.appendAssumeCapacity(@intCast(u8, ty.abiSize(target))); |
| 787 | 791 | // DW.AT.name, DW.FORM.string |
| 788 | try dbg_info_buffer.writer().print("{}\x00", .{ty}); | |
| 792 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(target)}); | |
| 789 | 793 | } else { |
| 790 | 794 | // Non-pointer optionals are structs: struct { .maybe = *, .val = * } |
| 791 | 795 | var buf = try arena.create(Type.Payload.ElemType); |
| ... | ... | @@ -796,7 +800,7 @@ fn addDbgInfoType( |
| 796 | 800 | const abi_size = ty.abiSize(target); |
| 797 | 801 | try leb128.writeULEB128(dbg_info_buffer.writer(), abi_size); |
| 798 | 802 | // DW.AT.name, DW.FORM.string |
| 799 | try dbg_info_buffer.writer().print("{}\x00", .{ty}); | |
| 803 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(target)}); | |
| 800 | 804 | // DW.AT.member |
| 801 | 805 | try dbg_info_buffer.ensureUnusedCapacity(7); |
| 802 | 806 | dbg_info_buffer.appendAssumeCapacity(abbrev_struct_member); |
| ... | ... | @@ -835,7 +839,7 @@ fn addDbgInfoType( |
| 835 | 839 | // DW.AT.byte_size, DW.FORM.sdata |
| 836 | 840 | dbg_info_buffer.appendAssumeCapacity(@sizeOf(usize) * 2); |
| 837 | 841 | // DW.AT.name, DW.FORM.string |
| 838 | try dbg_info_buffer.writer().print("{}\x00", .{ty}); | |
| 842 | try dbg_info_buffer.writer().print("{}\x00", .{ty.fmt(target)}); | |
| 839 | 843 | // DW.AT.member |
| 840 | 844 | try dbg_info_buffer.ensureUnusedCapacity(5); |
| 841 | 845 | dbg_info_buffer.appendAssumeCapacity(abbrev_struct_member); |
| ... | ... | @@ -882,7 +886,7 @@ fn addDbgInfoType( |
| 882 | 886 | const abi_size = ty.abiSize(target); |
| 883 | 887 | try leb128.writeULEB128(dbg_info_buffer.writer(), abi_size); |
| 884 | 888 | // DW.AT.name, DW.FORM.string |
| 885 | const struct_name = try ty.nameAllocArena(arena); | |
| 889 | const struct_name = try ty.nameAllocArena(arena, target); | |
| 886 | 890 | try dbg_info_buffer.ensureUnusedCapacity(struct_name.len + 1); |
| 887 | 891 | dbg_info_buffer.appendSliceAssumeCapacity(struct_name); |
| 888 | 892 | dbg_info_buffer.appendAssumeCapacity(0); |
| ... | ... | @@ -915,13 +919,15 @@ fn addDbgInfoType( |
| 915 | 919 | try dbg_info_buffer.append(0); |
| 916 | 920 | }, |
| 917 | 921 | else => { |
| 918 | log.debug("TODO implement .debug_info for type '{}'", .{ty}); | |
| 922 | log.debug("TODO implement .debug_info for type '{}'", .{ty.fmtDebug()}); | |
| 919 | 923 | try dbg_info_buffer.append(abbrev_pad1); |
| 920 | 924 | }, |
| 921 | 925 | } |
| 922 | 926 | |
| 923 | 927 | for (relocs.items) |rel| { |
| 924 | const gop = try dbg_info_type_relocs.getOrPut(self.allocator, rel.ty); | |
| 928 | const gop = try dbg_info_type_relocs.getOrPutContext(self.allocator, rel.ty, .{ | |
| 929 | .target = self.target, | |
| 930 | }); | |
| 925 | 931 | if (!gop.found_existing) { |
| 926 | 932 | gop.value_ptr.* = .{ |
| 927 | 933 | .off = undefined, |
src/link/MachO.zig+10-9| ... | ... | @@ -3874,7 +3874,7 @@ pub fn updateDecl(self: *MachO, module: *Module, decl: *Module.Decl) !void { |
| 3874 | 3874 | |
| 3875 | 3875 | /// Checks if the value, or any of its embedded values stores a pointer, and thus requires |
| 3876 | 3876 | /// a rebase opcode for the dynamic linker. |
| 3877 | fn needsPointerRebase(ty: Type, val: Value) bool { | |
| 3877 | fn needsPointerRebase(ty: Type, val: Value, target: std.Target) bool { | |
| 3878 | 3878 | if (ty.zigTypeTag() == .Fn) { |
| 3879 | 3879 | return false; |
| 3880 | 3880 | } |
| ... | ... | @@ -3890,7 +3890,7 @@ fn needsPointerRebase(ty: Type, val: Value) bool { |
| 3890 | 3890 | const elem_ty = ty.childType(); |
| 3891 | 3891 | var elem_value_buf: Value.ElemValueBuffer = undefined; |
| 3892 | 3892 | const elem_val = val.elemValueBuffer(0, &elem_value_buf); |
| 3893 | return needsPointerRebase(elem_ty, elem_val); | |
| 3893 | return needsPointerRebase(elem_ty, elem_val, target); | |
| 3894 | 3894 | }, |
| 3895 | 3895 | .Struct => { |
| 3896 | 3896 | const fields = ty.structFields().values(); |
| ... | ... | @@ -3898,7 +3898,7 @@ fn needsPointerRebase(ty: Type, val: Value) bool { |
| 3898 | 3898 | if (val.castTag(.aggregate)) |payload| { |
| 3899 | 3899 | const field_values = payload.data; |
| 3900 | 3900 | for (field_values) |field_val, i| { |
| 3901 | if (needsPointerRebase(fields[i].ty, field_val)) return true; | |
| 3901 | if (needsPointerRebase(fields[i].ty, field_val, target)) return true; | |
| 3902 | 3902 | } else return false; |
| 3903 | 3903 | } else return false; |
| 3904 | 3904 | }, |
| ... | ... | @@ -3907,18 +3907,18 @@ fn needsPointerRebase(ty: Type, val: Value) bool { |
| 3907 | 3907 | const sub_val = payload.data; |
| 3908 | 3908 | var buffer: Type.Payload.ElemType = undefined; |
| 3909 | 3909 | const sub_ty = ty.optionalChild(&buffer); |
| 3910 | return needsPointerRebase(sub_ty, sub_val); | |
| 3910 | return needsPointerRebase(sub_ty, sub_val, target); | |
| 3911 | 3911 | } else return false; |
| 3912 | 3912 | }, |
| 3913 | 3913 | .Union => { |
| 3914 | 3914 | const union_obj = val.cast(Value.Payload.Union).?.data; |
| 3915 | const active_field_ty = ty.unionFieldType(union_obj.tag); | |
| 3916 | return needsPointerRebase(active_field_ty, union_obj.val); | |
| 3915 | const active_field_ty = ty.unionFieldType(union_obj.tag, target); | |
| 3916 | return needsPointerRebase(active_field_ty, union_obj.val, target); | |
| 3917 | 3917 | }, |
| 3918 | 3918 | .ErrorUnion => { |
| 3919 | 3919 | if (val.castTag(.eu_payload)) |payload| { |
| 3920 | 3920 | const payload_ty = ty.errorUnionPayload(); |
| 3921 | return needsPointerRebase(payload_ty, payload.data); | |
| 3921 | return needsPointerRebase(payload_ty, payload.data, target); | |
| 3922 | 3922 | } else return false; |
| 3923 | 3923 | }, |
| 3924 | 3924 | else => return false, |
| ... | ... | @@ -3927,7 +3927,8 @@ fn needsPointerRebase(ty: Type, val: Value) bool { |
| 3927 | 3927 | |
| 3928 | 3928 | fn getMatchingSectionAtom(self: *MachO, atom: *Atom, name: []const u8, ty: Type, val: Value) !MatchingSection { |
| 3929 | 3929 | const code = atom.code.items; |
| 3930 | const alignment = ty.abiAlignment(self.base.options.target); | |
| 3930 | const target = self.base.options.target; | |
| 3931 | const alignment = ty.abiAlignment(target); | |
| 3931 | 3932 | const align_log_2 = math.log2(alignment); |
| 3932 | 3933 | const zig_ty = ty.zigTypeTag(); |
| 3933 | 3934 | const mode = self.base.options.optimize_mode; |
| ... | ... | @@ -3954,7 +3955,7 @@ fn getMatchingSectionAtom(self: *MachO, atom: *Atom, name: []const u8, ty: Type, |
| 3954 | 3955 | }; |
| 3955 | 3956 | } |
| 3956 | 3957 | |
| 3957 | if (needsPointerRebase(ty, val)) { | |
| 3958 | if (needsPointerRebase(ty, val, target)) { | |
| 3958 | 3959 | break :blk (try self.getMatchingSection(.{ |
| 3959 | 3960 | .segname = makeStaticString("__DATA_CONST"), |
| 3960 | 3961 | .sectname = makeStaticString("__const"), |
src/print_air.zig+6-6| ... | ... | @@ -299,12 +299,12 @@ const Writer = struct { |
| 299 | 299 | |
| 300 | 300 | fn writeTy(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { |
| 301 | 301 | const ty = w.air.instructions.items(.data)[inst].ty; |
| 302 | try s.print("{}", .{ty}); | |
| 302 | try s.print("{}", .{ty.fmtDebug()}); | |
| 303 | 303 | } |
| 304 | 304 | |
| 305 | 305 | fn writeTyOp(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { |
| 306 | 306 | const ty_op = w.air.instructions.items(.data)[inst].ty_op; |
| 307 | try s.print("{}, ", .{w.air.getRefType(ty_op.ty)}); | |
| 307 | try s.print("{}, ", .{w.air.getRefType(ty_op.ty).fmtDebug()}); | |
| 308 | 308 | try w.writeOperand(s, inst, 0, ty_op.operand); |
| 309 | 309 | } |
| 310 | 310 | |
| ... | ... | @@ -313,7 +313,7 @@ const Writer = struct { |
| 313 | 313 | const extra = w.air.extraData(Air.Block, ty_pl.payload); |
| 314 | 314 | const body = w.air.extra[extra.end..][0..extra.data.body_len]; |
| 315 | 315 | |
| 316 | try s.print("{}, {{\n", .{w.air.getRefType(ty_pl.ty)}); | |
| 316 | try s.print("{}, {{\n", .{w.air.getRefType(ty_pl.ty).fmtDebug()}); | |
| 317 | 317 | const old_indent = w.indent; |
| 318 | 318 | w.indent += 2; |
| 319 | 319 | try w.writeBody(s, body); |
| ... | ... | @@ -328,7 +328,7 @@ const Writer = struct { |
| 328 | 328 | const len = @intCast(usize, vector_ty.arrayLen()); |
| 329 | 329 | const elements = @bitCast([]const Air.Inst.Ref, w.air.extra[ty_pl.payload..][0..len]); |
| 330 | 330 | |
| 331 | try s.print("{}, [", .{vector_ty}); | |
| 331 | try s.print("{}, [", .{vector_ty.fmtDebug()}); | |
| 332 | 332 | for (elements) |elem, i| { |
| 333 | 333 | if (i != 0) try s.writeAll(", "); |
| 334 | 334 | try w.writeOperand(s, inst, i, elem); |
| ... | ... | @@ -502,7 +502,7 @@ const Writer = struct { |
| 502 | 502 | fn writeConstant(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { |
| 503 | 503 | const ty_pl = w.air.instructions.items(.data)[inst].ty_pl; |
| 504 | 504 | const val = w.air.values[ty_pl.payload]; |
| 505 | try s.print("{}, {}", .{ w.air.getRefType(ty_pl.ty), val.fmtDebug() }); | |
| 505 | try s.print("{}, {}", .{ w.air.getRefType(ty_pl.ty).fmtDebug(), val.fmtDebug() }); | |
| 506 | 506 | } |
| 507 | 507 | |
| 508 | 508 | fn writeAssembly(w: *Writer, s: anytype, inst: Air.Inst.Index) @TypeOf(s).Error!void { |
| ... | ... | @@ -514,7 +514,7 @@ const Writer = struct { |
| 514 | 514 | var op_index: usize = 0; |
| 515 | 515 | |
| 516 | 516 | const ret_ty = w.air.typeOfIndex(inst); |
| 517 | try s.print("{}", .{ret_ty}); | |
| 517 | try s.print("{}", .{ret_ty.fmtDebug()}); | |
| 518 | 518 | |
| 519 | 519 | if (is_volatile) { |
| 520 | 520 | try s.writeAll(", volatile"); |
src/type.zig+544-269| ... | ... | @@ -6,6 +6,7 @@ const Target = std.Target; |
| 6 | 6 | const Module = @import("Module.zig"); |
| 7 | 7 | const log = std.log.scoped(.Type); |
| 8 | 8 | const target_util = @import("target.zig"); |
| 9 | const TypedValue = @import("TypedValue.zig"); | |
| 9 | 10 | |
| 10 | 11 | const file_struct = @This(); |
| 11 | 12 | |
| ... | ... | @@ -520,7 +521,7 @@ pub const Type = extern union { |
| 520 | 521 | } |
| 521 | 522 | } |
| 522 | 523 | |
| 523 | pub fn eql(a: Type, b: Type) bool { | |
| 524 | pub fn eql(a: Type, b: Type, target: Target) bool { | |
| 524 | 525 | // As a shortcut, if the small tags / addresses match, we're done. |
| 525 | 526 | if (a.tag_if_small_enough == b.tag_if_small_enough) return true; |
| 526 | 527 | |
| ... | ... | @@ -636,7 +637,7 @@ pub const Type = extern union { |
| 636 | 637 | const a_info = a.fnInfo(); |
| 637 | 638 | const b_info = b.fnInfo(); |
| 638 | 639 | |
| 639 | if (!eql(a_info.return_type, b_info.return_type)) | |
| 640 | if (!eql(a_info.return_type, b_info.return_type, target)) | |
| 640 | 641 | return false; |
| 641 | 642 | |
| 642 | 643 | if (a_info.cc != b_info.cc) |
| ... | ... | @@ -662,7 +663,7 @@ pub const Type = extern union { |
| 662 | 663 | if (a_param_ty.tag() == .generic_poison) continue; |
| 663 | 664 | if (b_param_ty.tag() == .generic_poison) continue; |
| 664 | 665 | |
| 665 | if (!eql(a_param_ty, b_param_ty)) | |
| 666 | if (!eql(a_param_ty, b_param_ty, target)) | |
| 666 | 667 | return false; |
| 667 | 668 | } |
| 668 | 669 | |
| ... | ... | @@ -680,13 +681,13 @@ pub const Type = extern union { |
| 680 | 681 | if (a.arrayLen() != b.arrayLen()) |
| 681 | 682 | return false; |
| 682 | 683 | const elem_ty = a.elemType(); |
| 683 | if (!elem_ty.eql(b.elemType())) | |
| 684 | if (!elem_ty.eql(b.elemType(), target)) | |
| 684 | 685 | return false; |
| 685 | 686 | const sentinel_a = a.sentinel(); |
| 686 | 687 | const sentinel_b = b.sentinel(); |
| 687 | 688 | if (sentinel_a) |sa| { |
| 688 | 689 | if (sentinel_b) |sb| { |
| 689 | return sa.eql(sb, elem_ty); | |
| 690 | return sa.eql(sb, elem_ty, target); | |
| 690 | 691 | } else { |
| 691 | 692 | return false; |
| 692 | 693 | } |
| ... | ... | @@ -717,7 +718,7 @@ pub const Type = extern union { |
| 717 | 718 | |
| 718 | 719 | const info_a = a.ptrInfo().data; |
| 719 | 720 | const info_b = b.ptrInfo().data; |
| 720 | if (!info_a.pointee_type.eql(info_b.pointee_type)) | |
| 721 | if (!info_a.pointee_type.eql(info_b.pointee_type, target)) | |
| 721 | 722 | return false; |
| 722 | 723 | if (info_a.@"align" != info_b.@"align") |
| 723 | 724 | return false; |
| ... | ... | @@ -740,7 +741,7 @@ pub const Type = extern union { |
| 740 | 741 | const sentinel_b = info_b.sentinel; |
| 741 | 742 | if (sentinel_a) |sa| { |
| 742 | 743 | if (sentinel_b) |sb| { |
| 743 | if (!sa.eql(sb, info_a.pointee_type)) | |
| 744 | if (!sa.eql(sb, info_a.pointee_type, target)) | |
| 744 | 745 | return false; |
| 745 | 746 | } else { |
| 746 | 747 | return false; |
| ... | ... | @@ -761,7 +762,7 @@ pub const Type = extern union { |
| 761 | 762 | |
| 762 | 763 | var buf_a: Payload.ElemType = undefined; |
| 763 | 764 | var buf_b: Payload.ElemType = undefined; |
| 764 | return a.optionalChild(&buf_a).eql(b.optionalChild(&buf_b)); | |
| 765 | return a.optionalChild(&buf_a).eql(b.optionalChild(&buf_b), target); | |
| 765 | 766 | }, |
| 766 | 767 | |
| 767 | 768 | .anyerror_void_error_union, .error_union => { |
| ... | ... | @@ -769,18 +770,18 @@ pub const Type = extern union { |
| 769 | 770 | |
| 770 | 771 | const a_set = a.errorUnionSet(); |
| 771 | 772 | const b_set = b.errorUnionSet(); |
| 772 | if (!a_set.eql(b_set)) return false; | |
| 773 | if (!a_set.eql(b_set, target)) return false; | |
| 773 | 774 | |
| 774 | 775 | const a_payload = a.errorUnionPayload(); |
| 775 | 776 | const b_payload = b.errorUnionPayload(); |
| 776 | if (!a_payload.eql(b_payload)) return false; | |
| 777 | if (!a_payload.eql(b_payload, target)) return false; | |
| 777 | 778 | |
| 778 | 779 | return true; |
| 779 | 780 | }, |
| 780 | 781 | |
| 781 | 782 | .anyframe_T => { |
| 782 | 783 | if (b.zigTypeTag() != .AnyFrame) return false; |
| 783 | return a.childType().eql(b.childType()); | |
| 784 | return a.childType().eql(b.childType(), target); | |
| 784 | 785 | }, |
| 785 | 786 | |
| 786 | 787 | .empty_struct => { |
| ... | ... | @@ -803,7 +804,7 @@ pub const Type = extern union { |
| 803 | 804 | |
| 804 | 805 | for (a_tuple.types) |a_ty, i| { |
| 805 | 806 | const b_ty = b_tuple.types[i]; |
| 806 | if (!eql(a_ty, b_ty)) return false; | |
| 807 | if (!eql(a_ty, b_ty, target)) return false; | |
| 807 | 808 | } |
| 808 | 809 | |
| 809 | 810 | for (a_tuple.values) |a_val, i| { |
| ... | ... | @@ -819,7 +820,7 @@ pub const Type = extern union { |
| 819 | 820 | if (b_val.tag() == .unreachable_value) { |
| 820 | 821 | return false; |
| 821 | 822 | } else { |
| 822 | if (!Value.eql(a_val, b_val, ty)) return false; | |
| 823 | if (!Value.eql(a_val, b_val, ty, target)) return false; | |
| 823 | 824 | } |
| 824 | 825 | } |
| 825 | 826 | } |
| ... | ... | @@ -839,7 +840,7 @@ pub const Type = extern union { |
| 839 | 840 | |
| 840 | 841 | for (a_struct_obj.types) |a_ty, i| { |
| 841 | 842 | const b_ty = b_struct_obj.types[i]; |
| 842 | if (!eql(a_ty, b_ty)) return false; | |
| 843 | if (!eql(a_ty, b_ty, target)) return false; | |
| 843 | 844 | } |
| 844 | 845 | |
| 845 | 846 | for (a_struct_obj.values) |a_val, i| { |
| ... | ... | @@ -855,7 +856,7 @@ pub const Type = extern union { |
| 855 | 856 | if (b_val.tag() == .unreachable_value) { |
| 856 | 857 | return false; |
| 857 | 858 | } else { |
| 858 | if (!Value.eql(a_val, b_val, ty)) return false; | |
| 859 | if (!Value.eql(a_val, b_val, ty, target)) return false; | |
| 859 | 860 | } |
| 860 | 861 | } |
| 861 | 862 | } |
| ... | ... | @@ -910,13 +911,13 @@ pub const Type = extern union { |
| 910 | 911 | } |
| 911 | 912 | } |
| 912 | 913 | |
| 913 | pub fn hash(self: Type) u64 { | |
| 914 | pub fn hash(self: Type, target: Target) u64 { | |
| 914 | 915 | var hasher = std.hash.Wyhash.init(0); |
| 915 | self.hashWithHasher(&hasher); | |
| 916 | self.hashWithHasher(&hasher, target); | |
| 916 | 917 | return hasher.final(); |
| 917 | 918 | } |
| 918 | 919 | |
| 919 | pub fn hashWithHasher(ty: Type, hasher: *std.hash.Wyhash) void { | |
| 920 | pub fn hashWithHasher(ty: Type, hasher: *std.hash.Wyhash, target: Target) void { | |
| 920 | 921 | switch (ty.tag()) { |
| 921 | 922 | .generic_poison => unreachable, |
| 922 | 923 | |
| ... | ... | @@ -1035,7 +1036,7 @@ pub const Type = extern union { |
| 1035 | 1036 | std.hash.autoHash(hasher, std.builtin.TypeId.Fn); |
| 1036 | 1037 | |
| 1037 | 1038 | const fn_info = ty.fnInfo(); |
| 1038 | hashWithHasher(fn_info.return_type, hasher); | |
| 1039 | hashWithHasher(fn_info.return_type, hasher, target); | |
| 1039 | 1040 | std.hash.autoHash(hasher, fn_info.alignment); |
| 1040 | 1041 | std.hash.autoHash(hasher, fn_info.cc); |
| 1041 | 1042 | std.hash.autoHash(hasher, fn_info.is_var_args); |
| ... | ... | @@ -1045,7 +1046,7 @@ pub const Type = extern union { |
| 1045 | 1046 | for (fn_info.param_types) |param_ty, i| { |
| 1046 | 1047 | std.hash.autoHash(hasher, fn_info.paramIsComptime(i)); |
| 1047 | 1048 | if (param_ty.tag() == .generic_poison) continue; |
| 1048 | hashWithHasher(param_ty, hasher); | |
| 1049 | hashWithHasher(param_ty, hasher, target); | |
| 1049 | 1050 | } |
| 1050 | 1051 | }, |
| 1051 | 1052 | |
| ... | ... | @@ -1058,8 +1059,8 @@ pub const Type = extern union { |
| 1058 | 1059 | |
| 1059 | 1060 | const elem_ty = ty.elemType(); |
| 1060 | 1061 | std.hash.autoHash(hasher, ty.arrayLen()); |
| 1061 | hashWithHasher(elem_ty, hasher); | |
| 1062 | hashSentinel(ty.sentinel(), elem_ty, hasher); | |
| 1062 | hashWithHasher(elem_ty, hasher, target); | |
| 1063 | hashSentinel(ty.sentinel(), elem_ty, hasher, target); | |
| 1063 | 1064 | }, |
| 1064 | 1065 | |
| 1065 | 1066 | .vector => { |
| ... | ... | @@ -1067,7 +1068,7 @@ pub const Type = extern union { |
| 1067 | 1068 | |
| 1068 | 1069 | const elem_ty = ty.elemType(); |
| 1069 | 1070 | std.hash.autoHash(hasher, ty.vectorLen()); |
| 1070 | hashWithHasher(elem_ty, hasher); | |
| 1071 | hashWithHasher(elem_ty, hasher, target); | |
| 1071 | 1072 | }, |
| 1072 | 1073 | |
| 1073 | 1074 | .single_const_pointer_to_comptime_int, |
| ... | ... | @@ -1091,8 +1092,8 @@ pub const Type = extern union { |
| 1091 | 1092 | std.hash.autoHash(hasher, std.builtin.TypeId.Pointer); |
| 1092 | 1093 | |
| 1093 | 1094 | const info = ty.ptrInfo().data; |
| 1094 | hashWithHasher(info.pointee_type, hasher); | |
| 1095 | hashSentinel(info.sentinel, info.pointee_type, hasher); | |
| 1095 | hashWithHasher(info.pointee_type, hasher, target); | |
| 1096 | hashSentinel(info.sentinel, info.pointee_type, hasher, target); | |
| 1096 | 1097 | std.hash.autoHash(hasher, info.@"align"); |
| 1097 | 1098 | std.hash.autoHash(hasher, info.@"addrspace"); |
| 1098 | 1099 | std.hash.autoHash(hasher, info.bit_offset); |
| ... | ... | @@ -1110,22 +1111,22 @@ pub const Type = extern union { |
| 1110 | 1111 | std.hash.autoHash(hasher, std.builtin.TypeId.Optional); |
| 1111 | 1112 | |
| 1112 | 1113 | var buf: Payload.ElemType = undefined; |
| 1113 | hashWithHasher(ty.optionalChild(&buf), hasher); | |
| 1114 | hashWithHasher(ty.optionalChild(&buf), hasher, target); | |
| 1114 | 1115 | }, |
| 1115 | 1116 | |
| 1116 | 1117 | .anyerror_void_error_union, .error_union => { |
| 1117 | 1118 | std.hash.autoHash(hasher, std.builtin.TypeId.ErrorUnion); |
| 1118 | 1119 | |
| 1119 | 1120 | const set_ty = ty.errorUnionSet(); |
| 1120 | hashWithHasher(set_ty, hasher); | |
| 1121 | hashWithHasher(set_ty, hasher, target); | |
| 1121 | 1122 | |
| 1122 | 1123 | const payload_ty = ty.errorUnionPayload(); |
| 1123 | hashWithHasher(payload_ty, hasher); | |
| 1124 | hashWithHasher(payload_ty, hasher, target); | |
| 1124 | 1125 | }, |
| 1125 | 1126 | |
| 1126 | 1127 | .anyframe_T => { |
| 1127 | 1128 | std.hash.autoHash(hasher, std.builtin.TypeId.AnyFrame); |
| 1128 | hashWithHasher(ty.childType(), hasher); | |
| 1129 | hashWithHasher(ty.childType(), hasher, target); | |
| 1129 | 1130 | }, |
| 1130 | 1131 | |
| 1131 | 1132 | .empty_struct => { |
| ... | ... | @@ -1144,10 +1145,10 @@ pub const Type = extern union { |
| 1144 | 1145 | std.hash.autoHash(hasher, tuple.types.len); |
| 1145 | 1146 | |
| 1146 | 1147 | for (tuple.types) |field_ty, i| { |
| 1147 | hashWithHasher(field_ty, hasher); | |
| 1148 | hashWithHasher(field_ty, hasher, target); | |
| 1148 | 1149 | const field_val = tuple.values[i]; |
| 1149 | 1150 | if (field_val.tag() == .unreachable_value) continue; |
| 1150 | field_val.hash(field_ty, hasher); | |
| 1151 | field_val.hash(field_ty, hasher, target); | |
| 1151 | 1152 | } |
| 1152 | 1153 | }, |
| 1153 | 1154 | .anon_struct => { |
| ... | ... | @@ -1159,9 +1160,9 @@ pub const Type = extern union { |
| 1159 | 1160 | const field_name = struct_obj.names[i]; |
| 1160 | 1161 | const field_val = struct_obj.values[i]; |
| 1161 | 1162 | hasher.update(field_name); |
| 1162 | hashWithHasher(field_ty, hasher); | |
| 1163 | hashWithHasher(field_ty, hasher, target); | |
| 1163 | 1164 | if (field_val.tag() == .unreachable_value) continue; |
| 1164 | field_val.hash(field_ty, hasher); | |
| 1165 | field_val.hash(field_ty, hasher, target); | |
| 1165 | 1166 | } |
| 1166 | 1167 | }, |
| 1167 | 1168 | |
| ... | ... | @@ -1209,35 +1210,35 @@ pub const Type = extern union { |
| 1209 | 1210 | } |
| 1210 | 1211 | } |
| 1211 | 1212 | |
| 1212 | fn hashSentinel(opt_val: ?Value, ty: Type, hasher: *std.hash.Wyhash) void { | |
| 1213 | fn hashSentinel(opt_val: ?Value, ty: Type, hasher: *std.hash.Wyhash, target: Target) void { | |
| 1213 | 1214 | if (opt_val) |s| { |
| 1214 | 1215 | std.hash.autoHash(hasher, true); |
| 1215 | s.hash(ty, hasher); | |
| 1216 | s.hash(ty, hasher, target); | |
| 1216 | 1217 | } else { |
| 1217 | 1218 | std.hash.autoHash(hasher, false); |
| 1218 | 1219 | } |
| 1219 | 1220 | } |
| 1220 | 1221 | |
| 1221 | 1222 | pub const HashContext64 = struct { |
| 1223 | target: Target, | |
| 1224 | ||
| 1222 | 1225 | pub fn hash(self: @This(), t: Type) u64 { |
| 1223 | _ = self; | |
| 1224 | return t.hash(); | |
| 1226 | return t.hash(self.target); | |
| 1225 | 1227 | } |
| 1226 | 1228 | pub fn eql(self: @This(), a: Type, b: Type) bool { |
| 1227 | _ = self; | |
| 1228 | return a.eql(b); | |
| 1229 | return a.eql(b, self.target); | |
| 1229 | 1230 | } |
| 1230 | 1231 | }; |
| 1231 | 1232 | |
| 1232 | 1233 | pub const HashContext32 = struct { |
| 1234 | target: Target, | |
| 1235 | ||
| 1233 | 1236 | pub fn hash(self: @This(), t: Type) u32 { |
| 1234 | _ = self; | |
| 1235 | return @truncate(u32, t.hash()); | |
| 1237 | return @truncate(u32, t.hash(self.target)); | |
| 1236 | 1238 | } |
| 1237 | 1239 | pub fn eql(self: @This(), a: Type, b: Type, b_index: usize) bool { |
| 1238 | _ = self; | |
| 1239 | 1240 | _ = b_index; |
| 1240 | return a.eql(b); | |
| 1241 | return a.eql(b, self.target); | |
| 1241 | 1242 | } |
| 1242 | 1243 | }; |
| 1243 | 1244 | |
| ... | ... | @@ -1404,8 +1405,8 @@ pub const Type = extern union { |
| 1404 | 1405 | .function => { |
| 1405 | 1406 | const payload = self.castTag(.function).?.data; |
| 1406 | 1407 | const param_types = try allocator.alloc(Type, payload.param_types.len); |
| 1407 | for (payload.param_types) |param_type, i| { | |
| 1408 | param_types[i] = try param_type.copy(allocator); | |
| 1408 | for (payload.param_types) |param_ty, i| { | |
| 1409 | param_types[i] = try param_ty.copy(allocator); | |
| 1409 | 1410 | } |
| 1410 | 1411 | const other_comptime_params = payload.comptime_params[0..payload.param_types.len]; |
| 1411 | 1412 | const comptime_params = try allocator.dupe(bool, other_comptime_params); |
| ... | ... | @@ -1474,14 +1475,51 @@ pub const Type = extern union { |
| 1474 | 1475 | return Type{ .ptr_otherwise = &new_payload.base }; |
| 1475 | 1476 | } |
| 1476 | 1477 | |
| 1477 | pub fn format( | |
| 1478 | pub fn format(ty: Type, comptime unused_fmt_string: []const u8, options: std.fmt.FormatOptions, writer: anytype) !void { | |
| 1479 | _ = ty; | |
| 1480 | _ = unused_fmt_string; | |
| 1481 | _ = options; | |
| 1482 | _ = writer; | |
| 1483 | @compileError("do not format types directly; use either ty.fmtDebug() or ty.fmt()"); | |
| 1484 | } | |
| 1485 | ||
| 1486 | pub fn fmt(ty: Type, target: Target) std.fmt.Formatter(format2) { | |
| 1487 | return .{ .data = .{ | |
| 1488 | .ty = ty, | |
| 1489 | .target = target, | |
| 1490 | } }; | |
| 1491 | } | |
| 1492 | ||
| 1493 | const FormatContext = struct { | |
| 1494 | ty: Type, | |
| 1495 | target: Target, | |
| 1496 | }; | |
| 1497 | ||
| 1498 | fn format2( | |
| 1499 | ctx: FormatContext, | |
| 1500 | comptime unused_format_string: []const u8, | |
| 1501 | options: std.fmt.FormatOptions, | |
| 1502 | writer: anytype, | |
| 1503 | ) !void { | |
| 1504 | comptime assert(unused_format_string.len == 0); | |
| 1505 | _ = options; | |
| 1506 | return print(ctx.ty, writer, ctx.target); | |
| 1507 | } | |
| 1508 | ||
| 1509 | pub fn fmtDebug(ty: Type) std.fmt.Formatter(dump) { | |
| 1510 | return .{ .data = ty }; | |
| 1511 | } | |
| 1512 | ||
| 1513 | /// This is a debug function. In order to print types in a meaningful way | |
| 1514 | /// we also need access to the target. | |
| 1515 | pub fn dump( | |
| 1478 | 1516 | start_type: Type, |
| 1479 | comptime fmt: []const u8, | |
| 1517 | comptime unused_format_string: []const u8, | |
| 1480 | 1518 | options: std.fmt.FormatOptions, |
| 1481 | 1519 | writer: anytype, |
| 1482 | 1520 | ) @TypeOf(writer).Error!void { |
| 1483 | 1521 | _ = options; |
| 1484 | comptime assert(fmt.len == 0); | |
| 1522 | comptime assert(unused_format_string.len == 0); | |
| 1485 | 1523 | var ty = start_type; |
| 1486 | 1524 | while (true) { |
| 1487 | 1525 | const t = ty.tag(); |
| ... | ... | @@ -1584,7 +1622,7 @@ pub const Type = extern union { |
| 1584 | 1622 | try writer.writeAll("fn("); |
| 1585 | 1623 | for (payload.param_types) |param_type, i| { |
| 1586 | 1624 | if (i != 0) try writer.writeAll(", "); |
| 1587 | try param_type.format("", .{}, writer); | |
| 1625 | try param_type.dump("", .{}, writer); | |
| 1588 | 1626 | } |
| 1589 | 1627 | if (payload.is_var_args) { |
| 1590 | 1628 | if (payload.param_types.len != 0) { |
| ... | ... | @@ -1622,7 +1660,7 @@ pub const Type = extern union { |
| 1622 | 1660 | .vector => { |
| 1623 | 1661 | const payload = ty.castTag(.vector).?.data; |
| 1624 | 1662 | try writer.print("@Vector({d}, ", .{payload.len}); |
| 1625 | try payload.elem_type.format("", .{}, writer); | |
| 1663 | try payload.elem_type.dump("", .{}, writer); | |
| 1626 | 1664 | return writer.writeAll(")"); |
| 1627 | 1665 | }, |
| 1628 | 1666 | .array => { |
| ... | ... | @@ -1633,7 +1671,10 @@ pub const Type = extern union { |
| 1633 | 1671 | }, |
| 1634 | 1672 | .array_sentinel => { |
| 1635 | 1673 | const payload = ty.castTag(.array_sentinel).?.data; |
| 1636 | try writer.print("[{d}:{}]", .{ payload.len, payload.sentinel.fmtValue(payload.elem_type) }); | |
| 1674 | try writer.print("[{d}:{}]", .{ | |
| 1675 | payload.len, | |
| 1676 | payload.sentinel.fmtDebug(), | |
| 1677 | }); | |
| 1637 | 1678 | ty = payload.elem_type; |
| 1638 | 1679 | continue; |
| 1639 | 1680 | }, |
| ... | ... | @@ -1646,9 +1687,9 @@ pub const Type = extern union { |
| 1646 | 1687 | if (val.tag() != .unreachable_value) { |
| 1647 | 1688 | try writer.writeAll("comptime "); |
| 1648 | 1689 | } |
| 1649 | try field_ty.format("", .{}, writer); | |
| 1690 | try field_ty.dump("", .{}, writer); | |
| 1650 | 1691 | if (val.tag() != .unreachable_value) { |
| 1651 | try writer.print(" = {}", .{val.fmtValue(field_ty)}); | |
| 1692 | try writer.print(" = {}", .{val.fmtDebug()}); | |
| 1652 | 1693 | } |
| 1653 | 1694 | } |
| 1654 | 1695 | try writer.writeAll("}"); |
| ... | ... | @@ -1665,9 +1706,9 @@ pub const Type = extern union { |
| 1665 | 1706 | } |
| 1666 | 1707 | try writer.writeAll(anon_struct.names[i]); |
| 1667 | 1708 | try writer.writeAll(": "); |
| 1668 | try field_ty.format("", .{}, writer); | |
| 1709 | try field_ty.dump("", .{}, writer); | |
| 1669 | 1710 | if (val.tag() != .unreachable_value) { |
| 1670 | try writer.print(" = {}", .{val.fmtValue(field_ty)}); | |
| 1711 | try writer.print(" = {}", .{val.fmtDebug()}); | |
| 1671 | 1712 | } |
| 1672 | 1713 | } |
| 1673 | 1714 | try writer.writeAll("}"); |
| ... | ... | @@ -1752,8 +1793,8 @@ pub const Type = extern union { |
| 1752 | 1793 | const payload = ty.castTag(.pointer).?.data; |
| 1753 | 1794 | if (payload.sentinel) |some| switch (payload.size) { |
| 1754 | 1795 | .One, .C => unreachable, |
| 1755 | .Many => try writer.print("[*:{}]", .{some.fmtValue(payload.pointee_type)}), | |
| 1756 | .Slice => try writer.print("[:{}]", .{some.fmtValue(payload.pointee_type)}), | |
| 1796 | .Many => try writer.print("[*:{}]", .{some.fmtDebug()}), | |
| 1797 | .Slice => try writer.print("[:{}]", .{some.fmtDebug()}), | |
| 1757 | 1798 | } else switch (payload.size) { |
| 1758 | 1799 | .One => try writer.writeAll("*"), |
| 1759 | 1800 | .Many => try writer.writeAll("[*]"), |
| ... | ... | @@ -1780,7 +1821,7 @@ pub const Type = extern union { |
| 1780 | 1821 | }, |
| 1781 | 1822 | .error_union => { |
| 1782 | 1823 | const payload = ty.castTag(.error_union).?.data; |
| 1783 | try payload.error_set.format("", .{}, writer); | |
| 1824 | try payload.error_set.dump("", .{}, writer); | |
| 1784 | 1825 | try writer.writeAll("!"); |
| 1785 | 1826 | ty = payload.payload; |
| 1786 | 1827 | continue; |
| ... | ... | @@ -1821,20 +1862,17 @@ pub const Type = extern union { |
| 1821 | 1862 | } |
| 1822 | 1863 | } |
| 1823 | 1864 | |
| 1824 | pub fn nameAllocArena(ty: Type, arena: Allocator) Allocator.Error![:0]const u8 { | |
| 1825 | return nameAllocAdvanced(ty, arena, true); | |
| 1826 | } | |
| 1865 | pub const nameAllocArena = nameAlloc; | |
| 1827 | 1866 | |
| 1828 | pub fn nameAlloc(ty: Type, gpa: Allocator) Allocator.Error![:0]const u8 { | |
| 1829 | return nameAllocAdvanced(ty, gpa, false); | |
| 1867 | pub fn nameAlloc(ty: Type, ally: Allocator, target: Target) Allocator.Error![:0]const u8 { | |
| 1868 | var buffer = std.ArrayList(u8).init(ally); | |
| 1869 | defer buffer.deinit(); | |
| 1870 | try ty.print(buffer.writer(), target); | |
| 1871 | return buffer.toOwnedSliceSentinel(0); | |
| 1830 | 1872 | } |
| 1831 | 1873 | |
| 1832 | /// Returns a name suitable for `@typeName`. | |
| 1833 | pub fn nameAllocAdvanced( | |
| 1834 | ty: Type, | |
| 1835 | ally: Allocator, | |
| 1836 | is_arena: bool, | |
| 1837 | ) Allocator.Error![:0]const u8 { | |
| 1874 | /// Prints a name suitable for `@typeName`. | |
| 1875 | pub fn print(ty: Type, writer: anytype, target: Target) @TypeOf(writer).Error!void { | |
| 1838 | 1876 | const t = ty.tag(); |
| 1839 | 1877 | switch (t) { |
| 1840 | 1878 | .inferred_alloc_const => unreachable, |
| ... | ... | @@ -1892,141 +1930,251 @@ pub const Type = extern union { |
| 1892 | 1930 | .comptime_int, |
| 1893 | 1931 | .comptime_float, |
| 1894 | 1932 | .noreturn, |
| 1895 | => return maybeDupe(@tagName(t), ally, is_arena), | |
| 1933 | => try writer.writeAll(@tagName(t)), | |
| 1896 | 1934 | |
| 1897 | .enum_literal => return maybeDupe("@TypeOf(.enum_literal)", ally, is_arena), | |
| 1898 | .@"null" => return maybeDupe("@TypeOf(null)", ally, is_arena), | |
| 1899 | .@"undefined" => return maybeDupe("@TypeOf(undefined)", ally, is_arena), | |
| 1900 | .empty_struct_literal => return maybeDupe("@TypeOf(.{})", ally, is_arena), | |
| 1935 | .enum_literal => try writer.writeAll("@TypeOf(.enum_literal)"), | |
| 1936 | .@"null" => try writer.writeAll("@TypeOf(null)"), | |
| 1937 | .@"undefined" => try writer.writeAll("@TypeOf(undefined)"), | |
| 1938 | .empty_struct_literal => try writer.writeAll("@TypeOf(.{})"), | |
| 1901 | 1939 | |
| 1902 | 1940 | .empty_struct => { |
| 1903 | 1941 | const namespace = ty.castTag(.empty_struct).?.data; |
| 1904 | var buffer = std.ArrayList(u8).init(ally); | |
| 1905 | defer buffer.deinit(); | |
| 1906 | try namespace.renderFullyQualifiedName("", buffer.writer()); | |
| 1907 | return buffer.toOwnedSliceSentinel(0); | |
| 1942 | try namespace.renderFullyQualifiedName("", writer); | |
| 1908 | 1943 | }, |
| 1909 | 1944 | |
| 1910 | 1945 | .@"struct" => { |
| 1911 | 1946 | const struct_obj = ty.castTag(.@"struct").?.data; |
| 1912 | return try struct_obj.owner_decl.getFullyQualifiedName(ally); | |
| 1947 | try struct_obj.owner_decl.renderFullyQualifiedName(writer); | |
| 1913 | 1948 | }, |
| 1914 | 1949 | .@"union", .union_tagged => { |
| 1915 | 1950 | const union_obj = ty.cast(Payload.Union).?.data; |
| 1916 | return try union_obj.owner_decl.getFullyQualifiedName(ally); | |
| 1951 | try union_obj.owner_decl.renderFullyQualifiedName(writer); | |
| 1917 | 1952 | }, |
| 1918 | 1953 | .enum_full, .enum_nonexhaustive => { |
| 1919 | 1954 | const enum_full = ty.cast(Payload.EnumFull).?.data; |
| 1920 | return try enum_full.owner_decl.getFullyQualifiedName(ally); | |
| 1955 | try enum_full.owner_decl.renderFullyQualifiedName(writer); | |
| 1921 | 1956 | }, |
| 1922 | 1957 | .enum_simple => { |
| 1923 | 1958 | const enum_simple = ty.castTag(.enum_simple).?.data; |
| 1924 | return try enum_simple.owner_decl.getFullyQualifiedName(ally); | |
| 1959 | try enum_simple.owner_decl.renderFullyQualifiedName(writer); | |
| 1925 | 1960 | }, |
| 1926 | 1961 | .enum_numbered => { |
| 1927 | 1962 | const enum_numbered = ty.castTag(.enum_numbered).?.data; |
| 1928 | return try enum_numbered.owner_decl.getFullyQualifiedName(ally); | |
| 1963 | try enum_numbered.owner_decl.renderFullyQualifiedName(writer); | |
| 1929 | 1964 | }, |
| 1930 | 1965 | .@"opaque" => { |
| 1931 | 1966 | const opaque_obj = ty.cast(Payload.Opaque).?.data; |
| 1932 | return try opaque_obj.owner_decl.getFullyQualifiedName(ally); | |
| 1967 | try opaque_obj.owner_decl.renderFullyQualifiedName(writer); | |
| 1933 | 1968 | }, |
| 1934 | 1969 | |
| 1935 | .anyerror_void_error_union => return maybeDupe("anyerror!void", ally, is_arena), | |
| 1936 | .const_slice_u8 => return maybeDupe("[]const u8", ally, is_arena), | |
| 1937 | .const_slice_u8_sentinel_0 => return maybeDupe("[:0]const u8", ally, is_arena), | |
| 1938 | .fn_noreturn_no_args => return maybeDupe("fn() noreturn", ally, is_arena), | |
| 1939 | .fn_void_no_args => return maybeDupe("fn() void", ally, is_arena), | |
| 1940 | .fn_naked_noreturn_no_args => return maybeDupe("fn() callconv(.Naked) noreturn", ally, is_arena), | |
| 1941 | .fn_ccc_void_no_args => return maybeDupe("fn() callconv(.C) void", ally, is_arena), | |
| 1942 | .single_const_pointer_to_comptime_int => return maybeDupe("*const comptime_int", ally, is_arena), | |
| 1943 | .manyptr_u8 => return maybeDupe("[*]u8", ally, is_arena), | |
| 1944 | .manyptr_const_u8 => return maybeDupe("[*]const u8", ally, is_arena), | |
| 1945 | .manyptr_const_u8_sentinel_0 => return maybeDupe("[*:0]const u8", ally, is_arena), | |
| 1970 | .anyerror_void_error_union => try writer.writeAll("anyerror!void"), | |
| 1971 | .const_slice_u8 => try writer.writeAll("[]const u8"), | |
| 1972 | .const_slice_u8_sentinel_0 => try writer.writeAll("[:0]const u8"), | |
| 1973 | .fn_noreturn_no_args => try writer.writeAll("fn() noreturn"), | |
| 1974 | .fn_void_no_args => try writer.writeAll("fn() void"), | |
| 1975 | .fn_naked_noreturn_no_args => try writer.writeAll("fn() callconv(.Naked) noreturn"), | |
| 1976 | .fn_ccc_void_no_args => try writer.writeAll("fn() callconv(.C) void"), | |
| 1977 | .single_const_pointer_to_comptime_int => try writer.writeAll("*const comptime_int"), | |
| 1978 | .manyptr_u8 => try writer.writeAll("[*]u8"), | |
| 1979 | .manyptr_const_u8 => try writer.writeAll("[*]const u8"), | |
| 1980 | .manyptr_const_u8_sentinel_0 => try writer.writeAll("[*:0]const u8"), | |
| 1946 | 1981 | |
| 1947 | 1982 | .error_set_inferred => { |
| 1948 | 1983 | const func = ty.castTag(.error_set_inferred).?.data.func; |
| 1949 | 1984 | |
| 1950 | var buf = std.ArrayList(u8).init(ally); | |
| 1951 | defer buf.deinit(); | |
| 1952 | try buf.appendSlice("@typeInfo(@typeInfo(@TypeOf("); | |
| 1953 | try func.owner_decl.renderFullyQualifiedName(buf.writer()); | |
| 1954 | try buf.appendSlice(")).Fn.return_type.?).ErrorUnion.error_set"); | |
| 1955 | return try buf.toOwnedSliceSentinel(0); | |
| 1985 | try writer.writeAll("@typeInfo(@typeInfo(@TypeOf("); | |
| 1986 | try func.owner_decl.renderFullyQualifiedName(writer); | |
| 1987 | try writer.writeAll(")).Fn.return_type.?).ErrorUnion.error_set"); | |
| 1956 | 1988 | }, |
| 1957 | 1989 | |
| 1958 | 1990 | .function => { |
| 1959 | 1991 | const fn_info = ty.fnInfo(); |
| 1960 | var buf = std.ArrayList(u8).init(ally); | |
| 1961 | defer buf.deinit(); | |
| 1962 | try buf.appendSlice("fn("); | |
| 1963 | for (fn_info.param_types) |param_type, i| { | |
| 1964 | if (i != 0) try buf.appendSlice(", "); | |
| 1965 | const param_name = try param_type.nameAllocAdvanced(ally, is_arena); | |
| 1966 | defer if (!is_arena) ally.free(param_name); | |
| 1967 | try buf.appendSlice(param_name); | |
| 1992 | try writer.writeAll("fn("); | |
| 1993 | for (fn_info.param_types) |param_ty, i| { | |
| 1994 | if (i != 0) try writer.writeAll(", "); | |
| 1995 | try print(param_ty, writer, target); | |
| 1968 | 1996 | } |
| 1969 | 1997 | if (fn_info.is_var_args) { |
| 1970 | 1998 | if (fn_info.param_types.len != 0) { |
| 1971 | try buf.appendSlice(", "); | |
| 1999 | try writer.writeAll(", "); | |
| 1972 | 2000 | } |
| 1973 | try buf.appendSlice("..."); | |
| 2001 | try writer.writeAll("..."); | |
| 1974 | 2002 | } |
| 1975 | try buf.appendSlice(") "); | |
| 2003 | try writer.writeAll(") "); | |
| 1976 | 2004 | if (fn_info.cc != .Unspecified) { |
| 1977 | try buf.appendSlice("callconv(."); | |
| 1978 | try buf.appendSlice(@tagName(fn_info.cc)); | |
| 1979 | try buf.appendSlice(") "); | |
| 2005 | try writer.writeAll("callconv(."); | |
| 2006 | try writer.writeAll(@tagName(fn_info.cc)); | |
| 2007 | try writer.writeAll(") "); | |
| 1980 | 2008 | } |
| 1981 | 2009 | if (fn_info.alignment != 0) { |
| 1982 | try buf.writer().print("align({d}) ", .{fn_info.alignment}); | |
| 2010 | try writer.print("align({d}) ", .{fn_info.alignment}); | |
| 1983 | 2011 | } |
| 1984 | { | |
| 1985 | const ret_ty_name = try fn_info.return_type.nameAllocAdvanced(ally, is_arena); | |
| 1986 | defer if (!is_arena) ally.free(ret_ty_name); | |
| 1987 | try buf.appendSlice(ret_ty_name); | |
| 1988 | } | |
| 1989 | return try buf.toOwnedSliceSentinel(0); | |
| 2012 | try print(fn_info.return_type, writer, target); | |
| 1990 | 2013 | }, |
| 1991 | 2014 | |
| 1992 | 2015 | .error_union => { |
| 1993 | 2016 | const error_union = ty.castTag(.error_union).?.data; |
| 2017 | try print(error_union.error_set, writer, target); | |
| 2018 | try writer.writeAll("!"); | |
| 2019 | try print(error_union.payload, writer, target); | |
| 2020 | }, | |
| 1994 | 2021 | |
| 1995 | var buf = std.ArrayList(u8).init(ally); | |
| 1996 | defer buf.deinit(); | |
| 2022 | .array_u8 => { | |
| 2023 | const len = ty.castTag(.array_u8).?.data; | |
| 2024 | try writer.print("[{d}]u8", .{len}); | |
| 2025 | }, | |
| 2026 | .array_u8_sentinel_0 => { | |
| 2027 | const len = ty.castTag(.array_u8_sentinel_0).?.data; | |
| 2028 | try writer.print("[{d}:0]u8", .{len}); | |
| 2029 | }, | |
| 2030 | .vector => { | |
| 2031 | const payload = ty.castTag(.vector).?.data; | |
| 2032 | try writer.print("@Vector({d}, ", .{payload.len}); | |
| 2033 | try print(payload.elem_type, writer, target); | |
| 2034 | try writer.writeAll(")"); | |
| 2035 | }, | |
| 2036 | .array => { | |
| 2037 | const payload = ty.castTag(.array).?.data; | |
| 2038 | try writer.print("[{d}]", .{payload.len}); | |
| 2039 | try print(payload.elem_type, writer, target); | |
| 2040 | }, | |
| 2041 | .array_sentinel => { | |
| 2042 | const payload = ty.castTag(.array_sentinel).?.data; | |
| 2043 | try writer.print("[{d}:{}]", .{ | |
| 2044 | payload.len, | |
| 2045 | payload.sentinel.fmtValue(payload.elem_type, target), | |
| 2046 | }); | |
| 2047 | try print(payload.elem_type, writer, target); | |
| 2048 | }, | |
| 2049 | .tuple => { | |
| 2050 | const tuple = ty.castTag(.tuple).?.data; | |
| 1997 | 2051 | |
| 1998 | { | |
| 1999 | const err_set_ty_name = try error_union.error_set.nameAllocAdvanced(ally, is_arena); | |
| 2000 | defer if (!is_arena) ally.free(err_set_ty_name); | |
| 2001 | try buf.appendSlice(err_set_ty_name); | |
| 2052 | try writer.writeAll("tuple{"); | |
| 2053 | for (tuple.types) |field_ty, i| { | |
| 2054 | if (i != 0) try writer.writeAll(", "); | |
| 2055 | const val = tuple.values[i]; | |
| 2056 | if (val.tag() != .unreachable_value) { | |
| 2057 | try writer.writeAll("comptime "); | |
| 2058 | } | |
| 2059 | try print(field_ty, writer, target); | |
| 2060 | if (val.tag() != .unreachable_value) { | |
| 2061 | try writer.print(" = {}", .{val.fmtValue(field_ty, target)}); | |
| 2062 | } | |
| 2063 | } | |
| 2064 | try writer.writeAll("}"); | |
| 2065 | }, | |
| 2066 | .anon_struct => { | |
| 2067 | const anon_struct = ty.castTag(.anon_struct).?.data; | |
| 2068 | ||
| 2069 | try writer.writeAll("struct{"); | |
| 2070 | for (anon_struct.types) |field_ty, i| { | |
| 2071 | if (i != 0) try writer.writeAll(", "); | |
| 2072 | const val = anon_struct.values[i]; | |
| 2073 | if (val.tag() != .unreachable_value) { | |
| 2074 | try writer.writeAll("comptime "); | |
| 2075 | } | |
| 2076 | try writer.writeAll(anon_struct.names[i]); | |
| 2077 | try writer.writeAll(": "); | |
| 2078 | ||
| 2079 | try print(field_ty, writer, target); | |
| 2080 | ||
| 2081 | if (val.tag() != .unreachable_value) { | |
| 2082 | try writer.print(" = {}", .{val.fmtValue(field_ty, target)}); | |
| 2083 | } | |
| 2002 | 2084 | } |
| 2085 | try writer.writeAll("}"); | |
| 2086 | }, | |
| 2003 | 2087 | |
| 2004 | try buf.appendSlice("!"); | |
| 2088 | .pointer, | |
| 2089 | .single_const_pointer, | |
| 2090 | .single_mut_pointer, | |
| 2091 | .many_const_pointer, | |
| 2092 | .many_mut_pointer, | |
| 2093 | .c_const_pointer, | |
| 2094 | .c_mut_pointer, | |
| 2095 | .const_slice, | |
| 2096 | .mut_slice, | |
| 2097 | => { | |
| 2098 | const info = ty.ptrInfo().data; | |
| 2005 | 2099 | |
| 2006 | { | |
| 2007 | const payload_ty_name = try error_union.payload.nameAllocAdvanced(ally, is_arena); | |
| 2008 | defer if (!is_arena) ally.free(payload_ty_name); | |
| 2009 | try buf.appendSlice(payload_ty_name); | |
| 2100 | if (info.sentinel) |s| switch (info.size) { | |
| 2101 | .One, .C => unreachable, | |
| 2102 | .Many => try writer.print("[*:{}]", .{s.fmtValue(info.pointee_type, target)}), | |
| 2103 | .Slice => try writer.print("[:{}]", .{s.fmtValue(info.pointee_type, target)}), | |
| 2104 | } else switch (info.size) { | |
| 2105 | .One => try writer.writeAll("*"), | |
| 2106 | .Many => try writer.writeAll("[*]"), | |
| 2107 | .C => try writer.writeAll("[*c]"), | |
| 2108 | .Slice => try writer.writeAll("[]"), | |
| 2010 | 2109 | } |
| 2110 | if (info.@"align" != 0 or info.host_size != 0) { | |
| 2111 | try writer.print("align({d}", .{info.@"align"}); | |
| 2011 | 2112 | |
| 2012 | return try buf.toOwnedSliceSentinel(0); | |
| 2013 | }, | |
| 2113 | if (info.bit_offset != 0) { | |
| 2114 | try writer.print(":{d}:{d}", .{ info.bit_offset, info.host_size }); | |
| 2115 | } | |
| 2116 | try writer.writeAll(") "); | |
| 2117 | } | |
| 2118 | if (info.@"addrspace" != .generic) { | |
| 2119 | try writer.print("addrspace(.{s}) ", .{@tagName(info.@"addrspace")}); | |
| 2120 | } | |
| 2121 | if (!info.mutable) try writer.writeAll("const "); | |
| 2122 | if (info.@"volatile") try writer.writeAll("volatile "); | |
| 2123 | if (info.@"allowzero" and info.size != .C) try writer.writeAll("allowzero "); | |
| 2014 | 2124 | |
| 2015 | else => { | |
| 2016 | // TODO this is wasteful and also an incorrect implementation of `@typeName` | |
| 2017 | var buf = std.ArrayList(u8).init(ally); | |
| 2018 | defer buf.deinit(); | |
| 2019 | try buf.writer().print("{}", .{ty}); | |
| 2020 | return try buf.toOwnedSliceSentinel(0); | |
| 2125 | try print(info.pointee_type, writer, target); | |
| 2021 | 2126 | }, |
| 2022 | } | |
| 2023 | } | |
| 2024 | 2127 | |
| 2025 | fn maybeDupe(s: [:0]const u8, ally: Allocator, is_arena: bool) Allocator.Error![:0]const u8 { | |
| 2026 | if (is_arena) { | |
| 2027 | return s; | |
| 2028 | } else { | |
| 2029 | return try ally.dupeZ(u8, s); | |
| 2128 | .int_signed => { | |
| 2129 | const bits = ty.castTag(.int_signed).?.data; | |
| 2130 | return writer.print("i{d}", .{bits}); | |
| 2131 | }, | |
| 2132 | .int_unsigned => { | |
| 2133 | const bits = ty.castTag(.int_unsigned).?.data; | |
| 2134 | return writer.print("u{d}", .{bits}); | |
| 2135 | }, | |
| 2136 | .optional => { | |
| 2137 | const child_type = ty.castTag(.optional).?.data; | |
| 2138 | try writer.writeByte('?'); | |
| 2139 | try print(child_type, writer, target); | |
| 2140 | }, | |
| 2141 | .optional_single_mut_pointer => { | |
| 2142 | const pointee_type = ty.castTag(.optional_single_mut_pointer).?.data; | |
| 2143 | try writer.writeAll("?*"); | |
| 2144 | try print(pointee_type, writer, target); | |
| 2145 | }, | |
| 2146 | .optional_single_const_pointer => { | |
| 2147 | const pointee_type = ty.castTag(.optional_single_const_pointer).?.data; | |
| 2148 | try writer.writeAll("?*const "); | |
| 2149 | try print(pointee_type, writer, target); | |
| 2150 | }, | |
| 2151 | .anyframe_T => { | |
| 2152 | const return_type = ty.castTag(.anyframe_T).?.data; | |
| 2153 | try writer.print("anyframe->", .{}); | |
| 2154 | try print(return_type, writer, target); | |
| 2155 | }, | |
| 2156 | .error_set => { | |
| 2157 | const names = ty.castTag(.error_set).?.data.names.keys(); | |
| 2158 | try writer.writeAll("error{"); | |
| 2159 | for (names) |name, i| { | |
| 2160 | if (i != 0) try writer.writeByte(','); | |
| 2161 | try writer.writeAll(name); | |
| 2162 | } | |
| 2163 | try writer.writeAll("}"); | |
| 2164 | }, | |
| 2165 | .error_set_single => { | |
| 2166 | const name = ty.castTag(.error_set_single).?.data; | |
| 2167 | return writer.print("error{{{s}}}", .{name}); | |
| 2168 | }, | |
| 2169 | .error_set_merged => { | |
| 2170 | const names = ty.castTag(.error_set_merged).?.data.keys(); | |
| 2171 | try writer.writeAll("error{"); | |
| 2172 | for (names) |name, i| { | |
| 2173 | if (i != 0) try writer.writeByte(','); | |
| 2174 | try writer.writeAll(name); | |
| 2175 | } | |
| 2176 | try writer.writeAll("}"); | |
| 2177 | }, | |
| 2030 | 2178 | } |
| 2031 | 2179 | } |
| 2032 | 2180 | |
| ... | ... | @@ -2102,8 +2250,12 @@ pub const Type = extern union { |
| 2102 | 2250 | /// * the type has only one possible value, making its ABI size 0. |
| 2103 | 2251 | /// When `ignore_comptime_only` is true, then types that are comptime only |
| 2104 | 2252 | /// may return false positives. |
| 2105 | pub fn hasRuntimeBitsAdvanced(ty: Type, ignore_comptime_only: bool) bool { | |
| 2106 | return switch (ty.tag()) { | |
| 2253 | pub fn hasRuntimeBitsAdvanced( | |
| 2254 | ty: Type, | |
| 2255 | ignore_comptime_only: bool, | |
| 2256 | sema_kit: ?Module.WipAnalysis, | |
| 2257 | ) Module.CompileError!bool { | |
| 2258 | switch (ty.tag()) { | |
| 2107 | 2259 | .u1, |
| 2108 | 2260 | .u8, |
| 2109 | 2261 | .i8, |
| ... | ... | @@ -2157,7 +2309,7 @@ pub const Type = extern union { |
| 2157 | 2309 | .@"anyframe", |
| 2158 | 2310 | .anyopaque, |
| 2159 | 2311 | .@"opaque", |
| 2160 | => true, | |
| 2312 | => return true, | |
| 2161 | 2313 | |
| 2162 | 2314 | // These are false because they are comptime-only types. |
| 2163 | 2315 | .single_const_pointer_to_comptime_int, |
| ... | ... | @@ -2181,7 +2333,7 @@ pub const Type = extern union { |
| 2181 | 2333 | .fn_void_no_args, |
| 2182 | 2334 | .fn_naked_noreturn_no_args, |
| 2183 | 2335 | .fn_ccc_void_no_args, |
| 2184 | => false, | |
| 2336 | => return false, | |
| 2185 | 2337 | |
| 2186 | 2338 | // These types have more than one possible value, so the result is the same as |
| 2187 | 2339 | // asking whether they are comptime-only types. |
| ... | ... | @@ -2198,20 +2350,34 @@ pub const Type = extern union { |
| 2198 | 2350 | .const_slice, |
| 2199 | 2351 | .mut_slice, |
| 2200 | 2352 | .pointer, |
| 2201 | => if (ignore_comptime_only) true else !comptimeOnly(ty), | |
| 2353 | => { | |
| 2354 | if (ignore_comptime_only) { | |
| 2355 | return true; | |
| 2356 | } else if (sema_kit) |sk| { | |
| 2357 | return !(try sk.sema.typeRequiresComptime(sk.block, sk.src, ty)); | |
| 2358 | } else { | |
| 2359 | return !comptimeOnly(ty); | |
| 2360 | } | |
| 2361 | }, | |
| 2202 | 2362 | |
| 2203 | 2363 | .@"struct" => { |
| 2204 | 2364 | const struct_obj = ty.castTag(.@"struct").?.data; |
| 2365 | if (sema_kit) |sk| { | |
| 2366 | _ = try sk.sema.typeRequiresComptime(sk.block, sk.src, ty); | |
| 2367 | } | |
| 2205 | 2368 | switch (struct_obj.requires_comptime) { |
| 2206 | 2369 | .wip => unreachable, |
| 2207 | 2370 | .yes => return false, |
| 2208 | 2371 | .no => if (struct_obj.known_non_opv) return true, |
| 2209 | 2372 | .unknown => {}, |
| 2210 | 2373 | } |
| 2374 | if (sema_kit) |sk| { | |
| 2375 | _ = try sk.sema.resolveTypeFields(sk.block, sk.src, ty); | |
| 2376 | } | |
| 2211 | 2377 | assert(struct_obj.haveFieldTypes()); |
| 2212 | 2378 | for (struct_obj.fields.values()) |value| { |
| 2213 | 2379 | if (value.is_comptime) continue; |
| 2214 | if (value.ty.hasRuntimeBitsAdvanced(ignore_comptime_only)) | |
| 2380 | if (try value.ty.hasRuntimeBitsAdvanced(ignore_comptime_only, sema_kit)) | |
| 2215 | 2381 | return true; |
| 2216 | 2382 | } else { |
| 2217 | 2383 | return false; |
| ... | ... | @@ -2229,14 +2395,17 @@ pub const Type = extern union { |
| 2229 | 2395 | .enum_numbered, .enum_nonexhaustive => { |
| 2230 | 2396 | var buffer: Payload.Bits = undefined; |
| 2231 | 2397 | const int_tag_ty = ty.intTagType(&buffer); |
| 2232 | return int_tag_ty.hasRuntimeBitsAdvanced(ignore_comptime_only); | |
| 2398 | return int_tag_ty.hasRuntimeBitsAdvanced(ignore_comptime_only, sema_kit); | |
| 2233 | 2399 | }, |
| 2234 | 2400 | |
| 2235 | 2401 | .@"union" => { |
| 2236 | 2402 | const union_obj = ty.castTag(.@"union").?.data; |
| 2403 | if (sema_kit) |sk| { | |
| 2404 | _ = try sk.sema.resolveTypeFields(sk.block, sk.src, ty); | |
| 2405 | } | |
| 2237 | 2406 | assert(union_obj.haveFieldTypes()); |
| 2238 | 2407 | for (union_obj.fields.values()) |value| { |
| 2239 | if (value.ty.hasRuntimeBitsAdvanced(ignore_comptime_only)) | |
| 2408 | if (try value.ty.hasRuntimeBitsAdvanced(ignore_comptime_only, sema_kit)) | |
| 2240 | 2409 | return true; |
| 2241 | 2410 | } else { |
| 2242 | 2411 | return false; |
| ... | ... | @@ -2244,29 +2413,32 @@ pub const Type = extern union { |
| 2244 | 2413 | }, |
| 2245 | 2414 | .union_tagged => { |
| 2246 | 2415 | const union_obj = ty.castTag(.union_tagged).?.data; |
| 2247 | if (union_obj.tag_ty.hasRuntimeBitsAdvanced(ignore_comptime_only)) { | |
| 2416 | if (try union_obj.tag_ty.hasRuntimeBitsAdvanced(ignore_comptime_only, sema_kit)) { | |
| 2248 | 2417 | return true; |
| 2249 | 2418 | } |
| 2419 | if (sema_kit) |sk| { | |
| 2420 | _ = try sk.sema.resolveTypeFields(sk.block, sk.src, ty); | |
| 2421 | } | |
| 2250 | 2422 | assert(union_obj.haveFieldTypes()); |
| 2251 | 2423 | for (union_obj.fields.values()) |value| { |
| 2252 | if (value.ty.hasRuntimeBitsAdvanced(ignore_comptime_only)) | |
| 2424 | if (try value.ty.hasRuntimeBitsAdvanced(ignore_comptime_only, sema_kit)) | |
| 2253 | 2425 | return true; |
| 2254 | 2426 | } else { |
| 2255 | 2427 | return false; |
| 2256 | 2428 | } |
| 2257 | 2429 | }, |
| 2258 | 2430 | |
| 2259 | .array, .vector => ty.arrayLen() != 0 and | |
| 2260 | ty.elemType().hasRuntimeBitsAdvanced(ignore_comptime_only), | |
| 2261 | .array_u8 => ty.arrayLen() != 0, | |
| 2262 | .array_sentinel => ty.childType().hasRuntimeBitsAdvanced(ignore_comptime_only), | |
| 2431 | .array, .vector => return ty.arrayLen() != 0 and | |
| 2432 | try ty.elemType().hasRuntimeBitsAdvanced(ignore_comptime_only, sema_kit), | |
| 2433 | .array_u8 => return ty.arrayLen() != 0, | |
| 2434 | .array_sentinel => return ty.childType().hasRuntimeBitsAdvanced(ignore_comptime_only, sema_kit), | |
| 2263 | 2435 | |
| 2264 | .int_signed, .int_unsigned => ty.cast(Payload.Bits).?.data != 0, | |
| 2436 | .int_signed, .int_unsigned => return ty.cast(Payload.Bits).?.data != 0, | |
| 2265 | 2437 | |
| 2266 | 2438 | .error_union => { |
| 2267 | 2439 | const payload = ty.castTag(.error_union).?.data; |
| 2268 | return payload.error_set.hasRuntimeBitsAdvanced(ignore_comptime_only) or | |
| 2269 | payload.payload.hasRuntimeBitsAdvanced(ignore_comptime_only); | |
| 2440 | return (try payload.error_set.hasRuntimeBitsAdvanced(ignore_comptime_only, sema_kit)) or | |
| 2441 | (try payload.payload.hasRuntimeBitsAdvanced(ignore_comptime_only, sema_kit)); | |
| 2270 | 2442 | }, |
| 2271 | 2443 | |
| 2272 | 2444 | .tuple, .anon_struct => { |
| ... | ... | @@ -2274,7 +2446,7 @@ pub const Type = extern union { |
| 2274 | 2446 | for (tuple.types) |field_ty, i| { |
| 2275 | 2447 | const val = tuple.values[i]; |
| 2276 | 2448 | if (val.tag() != .unreachable_value) continue; // comptime field |
| 2277 | if (field_ty.hasRuntimeBitsAdvanced(ignore_comptime_only)) return true; | |
| 2449 | if (try field_ty.hasRuntimeBitsAdvanced(ignore_comptime_only, sema_kit)) return true; | |
| 2278 | 2450 | } |
| 2279 | 2451 | return false; |
| 2280 | 2452 | }, |
| ... | ... | @@ -2283,7 +2455,7 @@ pub const Type = extern union { |
| 2283 | 2455 | .inferred_alloc_mut => unreachable, |
| 2284 | 2456 | .var_args_param => unreachable, |
| 2285 | 2457 | .generic_poison => unreachable, |
| 2286 | }; | |
| 2458 | } | |
| 2287 | 2459 | } |
| 2288 | 2460 | |
| 2289 | 2461 | /// true if and only if the type has a well-defined memory layout |
| ... | ... | @@ -2409,11 +2581,11 @@ pub const Type = extern union { |
| 2409 | 2581 | } |
| 2410 | 2582 | |
| 2411 | 2583 | pub fn hasRuntimeBits(ty: Type) bool { |
| 2412 | return hasRuntimeBitsAdvanced(ty, false); | |
| 2584 | return hasRuntimeBitsAdvanced(ty, false, null) catch unreachable; | |
| 2413 | 2585 | } |
| 2414 | 2586 | |
| 2415 | 2587 | pub fn hasRuntimeBitsIgnoreComptime(ty: Type) bool { |
| 2416 | return hasRuntimeBitsAdvanced(ty, true); | |
| 2588 | return hasRuntimeBitsAdvanced(ty, true, null) catch unreachable; | |
| 2417 | 2589 | } |
| 2418 | 2590 | |
| 2419 | 2591 | pub fn isFnOrHasRuntimeBits(ty: Type) bool { |
| ... | ... | @@ -2518,8 +2690,33 @@ pub const Type = extern union { |
| 2518 | 2690 | } |
| 2519 | 2691 | |
| 2520 | 2692 | /// Returns 0 for 0-bit types. |
| 2521 | pub fn abiAlignment(self: Type, target: Target) u32 { | |
| 2522 | return switch (self.tag()) { | |
| 2693 | pub fn abiAlignment(ty: Type, target: Target) u32 { | |
| 2694 | return ty.abiAlignmentAdvanced(target, .eager).scalar; | |
| 2695 | } | |
| 2696 | ||
| 2697 | /// May capture a reference to `ty`. | |
| 2698 | pub fn lazyAbiAlignment(ty: Type, target: Target, arena: Allocator) !Value { | |
| 2699 | switch (ty.abiAlignmentAdvanced(target, .{ .lazy = arena })) { | |
| 2700 | .val => |val| return try val, | |
| 2701 | .scalar => |x| return Value.Tag.int_u64.create(arena, x), | |
| 2702 | } | |
| 2703 | } | |
| 2704 | ||
| 2705 | /// If you pass `eager` you will get back `scalar` and assert the type is resolved. | |
| 2706 | /// If you pass `lazy` you may get back `scalar` or `val`. | |
| 2707 | /// If `val` is returned, a reference to `ty` has been captured. | |
| 2708 | fn abiAlignmentAdvanced( | |
| 2709 | ty: Type, | |
| 2710 | target: Target, | |
| 2711 | strat: union(enum) { | |
| 2712 | eager, | |
| 2713 | lazy: Allocator, | |
| 2714 | }, | |
| 2715 | ) union(enum) { | |
| 2716 | scalar: u32, | |
| 2717 | val: Allocator.Error!Value, | |
| 2718 | } { | |
| 2719 | return switch (ty.tag()) { | |
| 2523 | 2720 | .u1, |
| 2524 | 2721 | .u8, |
| 2525 | 2722 | .i8, |
| ... | ... | @@ -2538,25 +2735,25 @@ pub const Type = extern union { |
| 2538 | 2735 | .extern_options, |
| 2539 | 2736 | .@"opaque", |
| 2540 | 2737 | .anyopaque, |
| 2541 | => return 1, | |
| 2738 | => return .{ .scalar = 1 }, | |
| 2542 | 2739 | |
| 2543 | 2740 | .fn_noreturn_no_args, // represents machine code; not a pointer |
| 2544 | 2741 | .fn_void_no_args, // represents machine code; not a pointer |
| 2545 | 2742 | .fn_naked_noreturn_no_args, // represents machine code; not a pointer |
| 2546 | 2743 | .fn_ccc_void_no_args, // represents machine code; not a pointer |
| 2547 | => return target_util.defaultFunctionAlignment(target), | |
| 2744 | => return .{ .scalar = target_util.defaultFunctionAlignment(target) }, | |
| 2548 | 2745 | |
| 2549 | 2746 | // represents machine code; not a pointer |
| 2550 | 2747 | .function => { |
| 2551 | const alignment = self.castTag(.function).?.data.alignment; | |
| 2552 | if (alignment != 0) return alignment; | |
| 2553 | return target_util.defaultFunctionAlignment(target); | |
| 2748 | const alignment = ty.castTag(.function).?.data.alignment; | |
| 2749 | if (alignment != 0) return .{ .scalar = alignment }; | |
| 2750 | return .{ .scalar = target_util.defaultFunctionAlignment(target) }; | |
| 2554 | 2751 | }, |
| 2555 | 2752 | |
| 2556 | .i16, .u16 => return 2, | |
| 2557 | .i32, .u32 => return 4, | |
| 2558 | .i64, .u64 => return 8, | |
| 2559 | .u128, .i128 => return 16, | |
| 2753 | .i16, .u16 => return .{ .scalar = 2 }, | |
| 2754 | .i32, .u32 => return .{ .scalar = 4 }, | |
| 2755 | .i64, .u64 => return .{ .scalar = 8 }, | |
| 2756 | .u128, .i128 => return .{ .scalar = 16 }, | |
| 2560 | 2757 | |
| 2561 | 2758 | .isize, |
| 2562 | 2759 | .usize, |
| ... | ... | @@ -2579,40 +2776,40 @@ pub const Type = extern union { |
| 2579 | 2776 | .manyptr_const_u8_sentinel_0, |
| 2580 | 2777 | .@"anyframe", |
| 2581 | 2778 | .anyframe_T, |
| 2582 | => return @divExact(target.cpu.arch.ptrBitWidth(), 8), | |
| 2583 | ||
| 2584 | .c_short => return @divExact(CType.short.sizeInBits(target), 8), | |
| 2585 | .c_ushort => return @divExact(CType.ushort.sizeInBits(target), 8), | |
| 2586 | .c_int => return @divExact(CType.int.sizeInBits(target), 8), | |
| 2587 | .c_uint => return @divExact(CType.uint.sizeInBits(target), 8), | |
| 2588 | .c_long => return @divExact(CType.long.sizeInBits(target), 8), | |
| 2589 | .c_ulong => return @divExact(CType.ulong.sizeInBits(target), 8), | |
| 2590 | .c_longlong => return @divExact(CType.longlong.sizeInBits(target), 8), | |
| 2591 | .c_ulonglong => return @divExact(CType.ulonglong.sizeInBits(target), 8), | |
| 2592 | ||
| 2593 | .f16 => return 2, | |
| 2594 | .f32 => return 4, | |
| 2595 | .f64 => return 8, | |
| 2596 | .f128 => return 16, | |
| 2779 | => return .{ .scalar = @divExact(target.cpu.arch.ptrBitWidth(), 8) }, | |
| 2780 | ||
| 2781 | .c_short => return .{ .scalar = @divExact(CType.short.sizeInBits(target), 8) }, | |
| 2782 | .c_ushort => return .{ .scalar = @divExact(CType.ushort.sizeInBits(target), 8) }, | |
| 2783 | .c_int => return .{ .scalar = @divExact(CType.int.sizeInBits(target), 8) }, | |
| 2784 | .c_uint => return .{ .scalar = @divExact(CType.uint.sizeInBits(target), 8) }, | |
| 2785 | .c_long => return .{ .scalar = @divExact(CType.long.sizeInBits(target), 8) }, | |
| 2786 | .c_ulong => return .{ .scalar = @divExact(CType.ulong.sizeInBits(target), 8) }, | |
| 2787 | .c_longlong => return .{ .scalar = @divExact(CType.longlong.sizeInBits(target), 8) }, | |
| 2788 | .c_ulonglong => return .{ .scalar = @divExact(CType.ulonglong.sizeInBits(target), 8) }, | |
| 2789 | ||
| 2790 | .f16 => return .{ .scalar = 2 }, | |
| 2791 | .f32 => return .{ .scalar = 4 }, | |
| 2792 | .f64 => return .{ .scalar = 8 }, | |
| 2793 | .f128 => return .{ .scalar = 16 }, | |
| 2597 | 2794 | |
| 2598 | 2795 | .f80 => switch (target.cpu.arch) { |
| 2599 | .i386 => return 4, | |
| 2600 | .x86_64 => return 16, | |
| 2796 | .i386 => return .{ .scalar = 4 }, | |
| 2797 | .x86_64 => return .{ .scalar = 16 }, | |
| 2601 | 2798 | else => { |
| 2602 | 2799 | var payload: Payload.Bits = .{ |
| 2603 | 2800 | .base = .{ .tag = .int_unsigned }, |
| 2604 | 2801 | .data = 80, |
| 2605 | 2802 | }; |
| 2606 | 2803 | const u80_ty = initPayload(&payload.base); |
| 2607 | return abiAlignment(u80_ty, target); | |
| 2804 | return .{ .scalar = abiAlignment(u80_ty, target) }; | |
| 2608 | 2805 | }, |
| 2609 | 2806 | }, |
| 2610 | 2807 | .c_longdouble => switch (CType.longdouble.sizeInBits(target)) { |
| 2611 | 16 => return abiAlignment(Type.f16, target), | |
| 2612 | 32 => return abiAlignment(Type.f32, target), | |
| 2613 | 64 => return abiAlignment(Type.f64, target), | |
| 2614 | 80 => return abiAlignment(Type.f80, target), | |
| 2615 | 128 => return abiAlignment(Type.f128, target), | |
| 2808 | 16 => return .{ .scalar = abiAlignment(Type.f16, target) }, | |
| 2809 | 32 => return .{ .scalar = abiAlignment(Type.f32, target) }, | |
| 2810 | 64 => return .{ .scalar = abiAlignment(Type.f64, target) }, | |
| 2811 | 80 => return .{ .scalar = abiAlignment(Type.f80, target) }, | |
| 2812 | 128 => return .{ .scalar = abiAlignment(Type.f128, target) }, | |
| 2616 | 2813 | else => unreachable, |
| 2617 | 2814 | }, |
| 2618 | 2815 | |
| ... | ... | @@ -2622,60 +2819,93 @@ pub const Type = extern union { |
| 2622 | 2819 | .anyerror, |
| 2623 | 2820 | .error_set_inferred, |
| 2624 | 2821 | .error_set_merged, |
| 2625 | => return 2, // TODO revisit this when we have the concept of the error tag type | |
| 2822 | => return .{ .scalar = 2 }, // TODO revisit this when we have the concept of the error tag type | |
| 2626 | 2823 | |
| 2627 | .array, .array_sentinel => return self.elemType().abiAlignment(target), | |
| 2824 | .array, .array_sentinel => return ty.elemType().abiAlignmentAdvanced(target, strat), | |
| 2628 | 2825 | |
| 2629 | 2826 | // TODO audit this - is there any more complicated logic to determine |
| 2630 | 2827 | // ABI alignment of vectors? |
| 2631 | .vector => return 16, | |
| 2828 | .vector => return .{ .scalar = 16 }, | |
| 2632 | 2829 | |
| 2633 | 2830 | .int_signed, .int_unsigned => { |
| 2634 | const bits: u16 = self.cast(Payload.Bits).?.data; | |
| 2635 | if (bits == 0) return 0; | |
| 2636 | if (bits <= 8) return 1; | |
| 2637 | if (bits <= 16) return 2; | |
| 2638 | if (bits <= 32) return 4; | |
| 2639 | if (bits <= 64) return 8; | |
| 2640 | return 16; | |
| 2831 | const bits: u16 = ty.cast(Payload.Bits).?.data; | |
| 2832 | if (bits == 0) return .{ .scalar = 0 }; | |
| 2833 | if (bits <= 8) return .{ .scalar = 1 }; | |
| 2834 | if (bits <= 16) return .{ .scalar = 2 }; | |
| 2835 | if (bits <= 32) return .{ .scalar = 4 }; | |
| 2836 | if (bits <= 64) return .{ .scalar = 8 }; | |
| 2837 | return .{ .scalar = 16 }; | |
| 2641 | 2838 | }, |
| 2642 | 2839 | |
| 2643 | 2840 | .optional => { |
| 2644 | 2841 | var buf: Payload.ElemType = undefined; |
| 2645 | const child_type = self.optionalChild(&buf); | |
| 2646 | if (!child_type.hasRuntimeBits()) return 1; | |
| 2842 | const child_type = ty.optionalChild(&buf); | |
| 2647 | 2843 | |
| 2648 | if (child_type.zigTypeTag() == .Pointer and !child_type.isCPtr()) | |
| 2649 | return @divExact(target.cpu.arch.ptrBitWidth(), 8); | |
| 2844 | if (child_type.zigTypeTag() == .Pointer and !child_type.isCPtr()) { | |
| 2845 | return .{ .scalar = @divExact(target.cpu.arch.ptrBitWidth(), 8) }; | |
| 2846 | } | |
| 2650 | 2847 | |
| 2651 | return child_type.abiAlignment(target); | |
| 2848 | switch (strat) { | |
| 2849 | .eager => { | |
| 2850 | if (!child_type.hasRuntimeBits()) return .{ .scalar = 1 }; | |
| 2851 | return .{ .scalar = child_type.abiAlignment(target) }; | |
| 2852 | }, | |
| 2853 | .lazy => |arena| switch (child_type.abiAlignmentAdvanced(target, strat)) { | |
| 2854 | .scalar => |x| return .{ .scalar = @maximum(x, 1) }, | |
| 2855 | .val => return .{ .val = Value.Tag.lazy_align.create(arena, ty) }, | |
| 2856 | }, | |
| 2857 | } | |
| 2652 | 2858 | }, |
| 2653 | 2859 | |
| 2654 | 2860 | .error_union => { |
| 2655 | const data = self.castTag(.error_union).?.data; | |
| 2656 | if (!data.error_set.hasRuntimeBits()) { | |
| 2657 | return data.payload.abiAlignment(target); | |
| 2658 | } else if (!data.payload.hasRuntimeBits()) { | |
| 2659 | return data.error_set.abiAlignment(target); | |
| 2861 | const data = ty.castTag(.error_union).?.data; | |
| 2862 | switch (strat) { | |
| 2863 | .eager => { | |
| 2864 | if (!data.error_set.hasRuntimeBits()) { | |
| 2865 | return .{ .scalar = data.payload.abiAlignment(target) }; | |
| 2866 | } else if (!data.payload.hasRuntimeBits()) { | |
| 2867 | return .{ .scalar = data.error_set.abiAlignment(target) }; | |
| 2868 | } | |
| 2869 | return .{ .scalar = @maximum( | |
| 2870 | data.payload.abiAlignment(target), | |
| 2871 | data.error_set.abiAlignment(target), | |
| 2872 | ) }; | |
| 2873 | }, | |
| 2874 | .lazy => |arena| { | |
| 2875 | switch (data.payload.abiAlignmentAdvanced(target, strat)) { | |
| 2876 | .scalar => |payload_align| { | |
| 2877 | if (payload_align == 0) { | |
| 2878 | return data.error_set.abiAlignmentAdvanced(target, strat); | |
| 2879 | } | |
| 2880 | switch (data.error_set.abiAlignmentAdvanced(target, strat)) { | |
| 2881 | .scalar => |err_set_align| { | |
| 2882 | return .{ .scalar = @maximum(payload_align, err_set_align) }; | |
| 2883 | }, | |
| 2884 | .val => {}, | |
| 2885 | } | |
| 2886 | }, | |
| 2887 | .val => {}, | |
| 2888 | } | |
| 2889 | return .{ .val = Value.Tag.lazy_align.create(arena, ty) }; | |
| 2890 | }, | |
| 2660 | 2891 | } |
| 2661 | return @maximum( | |
| 2662 | data.payload.abiAlignment(target), | |
| 2663 | data.error_set.abiAlignment(target), | |
| 2664 | ); | |
| 2665 | 2892 | }, |
| 2666 | 2893 | |
| 2667 | 2894 | .@"struct" => { |
| 2668 | const fields = self.structFields(); | |
| 2669 | if (self.castTag(.@"struct")) |payload| { | |
| 2895 | if (ty.castTag(.@"struct")) |payload| { | |
| 2670 | 2896 | const struct_obj = payload.data; |
| 2671 | assert(struct_obj.haveLayout()); | |
| 2897 | if (!struct_obj.haveLayout()) switch (strat) { | |
| 2898 | .eager => unreachable, // struct layout not resolved | |
| 2899 | .lazy => |arena| return .{ .val = Value.Tag.lazy_align.create(arena, ty) }, | |
| 2900 | }; | |
| 2672 | 2901 | if (struct_obj.layout == .Packed) { |
| 2673 | 2902 | var buf: Type.Payload.Bits = undefined; |
| 2674 | 2903 | const int_ty = struct_obj.packedIntegerType(target, &buf); |
| 2675 | return int_ty.abiAlignment(target); | |
| 2904 | return .{ .scalar = int_ty.abiAlignment(target) }; | |
| 2676 | 2905 | } |
| 2677 | 2906 | } |
| 2678 | 2907 | |
| 2908 | const fields = ty.structFields(); | |
| 2679 | 2909 | var big_align: u32 = 0; |
| 2680 | 2910 | for (fields.values()) |field| { |
| 2681 | 2911 | if (!field.ty.hasRuntimeBits()) continue; |
| ... | ... | @@ -2683,31 +2913,45 @@ pub const Type = extern union { |
| 2683 | 2913 | const field_align = field.normalAlignment(target); |
| 2684 | 2914 | big_align = @maximum(big_align, field_align); |
| 2685 | 2915 | } |
| 2686 | return big_align; | |
| 2916 | return .{ .scalar = big_align }; | |
| 2687 | 2917 | }, |
| 2688 | 2918 | |
| 2689 | 2919 | .tuple, .anon_struct => { |
| 2690 | const tuple = self.tupleFields(); | |
| 2920 | const tuple = ty.tupleFields(); | |
| 2691 | 2921 | var big_align: u32 = 0; |
| 2692 | 2922 | for (tuple.types) |field_ty, i| { |
| 2693 | 2923 | const val = tuple.values[i]; |
| 2694 | 2924 | if (val.tag() != .unreachable_value) continue; // comptime field |
| 2695 | if (!field_ty.hasRuntimeBits()) continue; | |
| 2696 | 2925 | |
| 2697 | const field_align = field_ty.abiAlignment(target); | |
| 2698 | big_align = @maximum(big_align, field_align); | |
| 2926 | switch (field_ty.abiAlignmentAdvanced(target, strat)) { | |
| 2927 | .scalar => |field_align| big_align = @maximum(big_align, field_align), | |
| 2928 | .val => switch (strat) { | |
| 2929 | .eager => unreachable, // field type alignment not resolved | |
| 2930 | .lazy => |arena| return .{ .val = Value.Tag.lazy_align.create(arena, ty) }, | |
| 2931 | }, | |
| 2932 | } | |
| 2699 | 2933 | } |
| 2700 | return big_align; | |
| 2934 | return .{ .scalar = big_align }; | |
| 2701 | 2935 | }, |
| 2702 | 2936 | |
| 2703 | 2937 | .enum_full, .enum_nonexhaustive, .enum_simple, .enum_numbered => { |
| 2704 | 2938 | var buffer: Payload.Bits = undefined; |
| 2705 | const int_tag_ty = self.intTagType(&buffer); | |
| 2706 | return int_tag_ty.abiAlignment(target); | |
| 2939 | const int_tag_ty = ty.intTagType(&buffer); | |
| 2940 | return .{ .scalar = int_tag_ty.abiAlignment(target) }; | |
| 2941 | }, | |
| 2942 | .@"union" => switch (strat) { | |
| 2943 | .eager => { | |
| 2944 | // TODO pass `true` for have_tag when unions have a safety tag | |
| 2945 | return .{ .scalar = ty.castTag(.@"union").?.data.abiAlignment(target, false) }; | |
| 2946 | }, | |
| 2947 | .lazy => |arena| return .{ .val = Value.Tag.lazy_align.create(arena, ty) }, | |
| 2948 | }, | |
| 2949 | .union_tagged => switch (strat) { | |
| 2950 | .eager => { | |
| 2951 | return .{ .scalar = ty.castTag(.union_tagged).?.data.abiAlignment(target, true) }; | |
| 2952 | }, | |
| 2953 | .lazy => |arena| return .{ .val = Value.Tag.lazy_align.create(arena, ty) }, | |
| 2707 | 2954 | }, |
| 2708 | // TODO pass `true` for have_tag when unions have a safety tag | |
| 2709 | .@"union" => return self.castTag(.@"union").?.data.abiAlignment(target, false), | |
| 2710 | .union_tagged => return self.castTag(.union_tagged).?.data.abiAlignment(target, true), | |
| 2711 | 2955 | |
| 2712 | 2956 | .empty_struct, |
| 2713 | 2957 | .void, |
| ... | ... | @@ -2719,7 +2963,7 @@ pub const Type = extern union { |
| 2719 | 2963 | .@"undefined", |
| 2720 | 2964 | .enum_literal, |
| 2721 | 2965 | .type_info, |
| 2722 | => return 0, | |
| 2966 | => return .{ .scalar = 0 }, | |
| 2723 | 2967 | |
| 2724 | 2968 | .noreturn, |
| 2725 | 2969 | .inferred_alloc_const, |
| ... | ... | @@ -3392,10 +3636,7 @@ pub const Type = extern union { |
| 3392 | 3636 | |
| 3393 | 3637 | .optional => { |
| 3394 | 3638 | const child_ty = self.castTag(.optional).?.data; |
| 3395 | // optionals of zero sized types behave like bools, not pointers | |
| 3396 | if (!child_ty.hasRuntimeBits()) return false; | |
| 3397 | 3639 | if (child_ty.zigTypeTag() != .Pointer) return false; |
| 3398 | ||
| 3399 | 3640 | const info = child_ty.ptrInfo().data; |
| 3400 | 3641 | switch (info.size) { |
| 3401 | 3642 | .Slice, .C => return false, |
| ... | ... | @@ -3663,9 +3904,9 @@ pub const Type = extern union { |
| 3663 | 3904 | return union_obj.fields; |
| 3664 | 3905 | } |
| 3665 | 3906 | |
| 3666 | pub fn unionFieldType(ty: Type, enum_tag: Value) Type { | |
| 3907 | pub fn unionFieldType(ty: Type, enum_tag: Value, target: Target) Type { | |
| 3667 | 3908 | const union_obj = ty.cast(Payload.Union).?.data; |
| 3668 | const index = union_obj.tag_ty.enumTagFieldIndex(enum_tag).?; | |
| 3909 | const index = union_obj.tag_ty.enumTagFieldIndex(enum_tag, target).?; | |
| 3669 | 3910 | assert(union_obj.haveFieldTypes()); |
| 3670 | 3911 | return union_obj.fields.values()[index].ty; |
| 3671 | 3912 | } |
| ... | ... | @@ -4330,6 +4571,8 @@ pub const Type = extern union { |
| 4330 | 4571 | |
| 4331 | 4572 | /// During semantic analysis, instead call `Sema.typeRequiresComptime` which |
| 4332 | 4573 | /// resolves field types rather than asserting they are already resolved. |
| 4574 | /// TODO merge these implementations together with the "advanced" pattern seen | |
| 4575 | /// elsewhere in this file. | |
| 4333 | 4576 | pub fn comptimeOnly(ty: Type) bool { |
| 4334 | 4577 | return switch (ty.tag()) { |
| 4335 | 4578 | .u1, |
| ... | ... | @@ -4679,20 +4922,20 @@ pub const Type = extern union { |
| 4679 | 4922 | /// Asserts `ty` is an enum. `enum_tag` can either be `enum_field_index` or |
| 4680 | 4923 | /// an integer which represents the enum value. Returns the field index in |
| 4681 | 4924 | /// declaration order, or `null` if `enum_tag` does not match any field. |
| 4682 | pub fn enumTagFieldIndex(ty: Type, enum_tag: Value) ?usize { | |
| 4925 | pub fn enumTagFieldIndex(ty: Type, enum_tag: Value, target: Target) ?usize { | |
| 4683 | 4926 | if (enum_tag.castTag(.enum_field_index)) |payload| { |
| 4684 | 4927 | return @as(usize, payload.data); |
| 4685 | 4928 | } |
| 4686 | 4929 | const S = struct { |
| 4687 | fn fieldWithRange(int_ty: Type, int_val: Value, end: usize) ?usize { | |
| 4930 | fn fieldWithRange(int_ty: Type, int_val: Value, end: usize, tg: Target) ?usize { | |
| 4688 | 4931 | if (int_val.compareWithZero(.lt)) return null; |
| 4689 | 4932 | var end_payload: Value.Payload.U64 = .{ |
| 4690 | 4933 | .base = .{ .tag = .int_u64 }, |
| 4691 | 4934 | .data = end, |
| 4692 | 4935 | }; |
| 4693 | 4936 | const end_val = Value.initPayload(&end_payload.base); |
| 4694 | if (int_val.compare(.gte, end_val, int_ty)) return null; | |
| 4695 | return @intCast(usize, int_val.toUnsignedInt()); | |
| 4937 | if (int_val.compare(.gte, end_val, int_ty, tg)) return null; | |
| 4938 | return @intCast(usize, int_val.toUnsignedInt(tg)); | |
| 4696 | 4939 | } |
| 4697 | 4940 | }; |
| 4698 | 4941 | switch (ty.tag()) { |
| ... | ... | @@ -4700,18 +4943,24 @@ pub const Type = extern union { |
| 4700 | 4943 | const enum_full = ty.cast(Payload.EnumFull).?.data; |
| 4701 | 4944 | const tag_ty = enum_full.tag_ty; |
| 4702 | 4945 | if (enum_full.values.count() == 0) { |
| 4703 | return S.fieldWithRange(tag_ty, enum_tag, enum_full.fields.count()); | |
| 4946 | return S.fieldWithRange(tag_ty, enum_tag, enum_full.fields.count(), target); | |
| 4704 | 4947 | } else { |
| 4705 | return enum_full.values.getIndexContext(enum_tag, .{ .ty = tag_ty }); | |
| 4948 | return enum_full.values.getIndexContext(enum_tag, .{ | |
| 4949 | .ty = tag_ty, | |
| 4950 | .target = target, | |
| 4951 | }); | |
| 4706 | 4952 | } |
| 4707 | 4953 | }, |
| 4708 | 4954 | .enum_numbered => { |
| 4709 | 4955 | const enum_obj = ty.castTag(.enum_numbered).?.data; |
| 4710 | 4956 | const tag_ty = enum_obj.tag_ty; |
| 4711 | 4957 | if (enum_obj.values.count() == 0) { |
| 4712 | return S.fieldWithRange(tag_ty, enum_tag, enum_obj.fields.count()); | |
| 4958 | return S.fieldWithRange(tag_ty, enum_tag, enum_obj.fields.count(), target); | |
| 4713 | 4959 | } else { |
| 4714 | return enum_obj.values.getIndexContext(enum_tag, .{ .ty = tag_ty }); | |
| 4960 | return enum_obj.values.getIndexContext(enum_tag, .{ | |
| 4961 | .ty = tag_ty, | |
| 4962 | .target = target, | |
| 4963 | }); | |
| 4715 | 4964 | } |
| 4716 | 4965 | }, |
| 4717 | 4966 | .enum_simple => { |
| ... | ... | @@ -4723,7 +4972,7 @@ pub const Type = extern union { |
| 4723 | 4972 | .data = bits, |
| 4724 | 4973 | }; |
| 4725 | 4974 | const tag_ty = Type.initPayload(&buffer.base); |
| 4726 | return S.fieldWithRange(tag_ty, enum_tag, fields_len); | |
| 4975 | return S.fieldWithRange(tag_ty, enum_tag, fields_len, target); | |
| 4727 | 4976 | }, |
| 4728 | 4977 | .atomic_order, |
| 4729 | 4978 | .atomic_rmw_op, |
| ... | ... | @@ -5018,14 +5267,14 @@ pub const Type = extern union { |
| 5018 | 5267 | /// Asserts the type is an enum. |
| 5019 | 5268 | pub fn enumHasInt(ty: Type, int: Value, target: Target) bool { |
| 5020 | 5269 | const S = struct { |
| 5021 | fn intInRange(tag_ty: Type, int_val: Value, end: usize) bool { | |
| 5270 | fn intInRange(tag_ty: Type, int_val: Value, end: usize, tg: Target) bool { | |
| 5022 | 5271 | if (int_val.compareWithZero(.lt)) return false; |
| 5023 | 5272 | var end_payload: Value.Payload.U64 = .{ |
| 5024 | 5273 | .base = .{ .tag = .int_u64 }, |
| 5025 | 5274 | .data = end, |
| 5026 | 5275 | }; |
| 5027 | 5276 | const end_val = Value.initPayload(&end_payload.base); |
| 5028 | if (int_val.compare(.gte, end_val, tag_ty)) return false; | |
| 5277 | if (int_val.compare(.gte, end_val, tag_ty, tg)) return false; | |
| 5029 | 5278 | return true; |
| 5030 | 5279 | } |
| 5031 | 5280 | }; |
| ... | ... | @@ -5035,18 +5284,24 @@ pub const Type = extern union { |
| 5035 | 5284 | const enum_full = ty.castTag(.enum_full).?.data; |
| 5036 | 5285 | const tag_ty = enum_full.tag_ty; |
| 5037 | 5286 | if (enum_full.values.count() == 0) { |
| 5038 | return S.intInRange(tag_ty, int, enum_full.fields.count()); | |
| 5287 | return S.intInRange(tag_ty, int, enum_full.fields.count(), target); | |
| 5039 | 5288 | } else { |
| 5040 | return enum_full.values.containsContext(int, .{ .ty = tag_ty }); | |
| 5289 | return enum_full.values.containsContext(int, .{ | |
| 5290 | .ty = tag_ty, | |
| 5291 | .target = target, | |
| 5292 | }); | |
| 5041 | 5293 | } |
| 5042 | 5294 | }, |
| 5043 | 5295 | .enum_numbered => { |
| 5044 | 5296 | const enum_obj = ty.castTag(.enum_numbered).?.data; |
| 5045 | 5297 | const tag_ty = enum_obj.tag_ty; |
| 5046 | 5298 | if (enum_obj.values.count() == 0) { |
| 5047 | return S.intInRange(tag_ty, int, enum_obj.fields.count()); | |
| 5299 | return S.intInRange(tag_ty, int, enum_obj.fields.count(), target); | |
| 5048 | 5300 | } else { |
| 5049 | return enum_obj.values.containsContext(int, .{ .ty = tag_ty }); | |
| 5301 | return enum_obj.values.containsContext(int, .{ | |
| 5302 | .ty = tag_ty, | |
| 5303 | .target = target, | |
| 5304 | }); | |
| 5050 | 5305 | } |
| 5051 | 5306 | }, |
| 5052 | 5307 | .enum_simple => { |
| ... | ... | @@ -5058,7 +5313,7 @@ pub const Type = extern union { |
| 5058 | 5313 | .data = bits, |
| 5059 | 5314 | }; |
| 5060 | 5315 | const tag_ty = Type.initPayload(&buffer.base); |
| 5061 | return S.intInRange(tag_ty, int, fields_len); | |
| 5316 | return S.intInRange(tag_ty, int, fields_len, target); | |
| 5062 | 5317 | }, |
| 5063 | 5318 | .atomic_order, |
| 5064 | 5319 | .atomic_rmw_op, |
| ... | ... | @@ -5070,7 +5325,7 @@ pub const Type = extern union { |
| 5070 | 5325 | .prefetch_options, |
| 5071 | 5326 | .export_options, |
| 5072 | 5327 | .extern_options, |
| 5073 | => @panic("TODO resolve std.builtin types"), | |
| 5328 | => unreachable, | |
| 5074 | 5329 | |
| 5075 | 5330 | else => unreachable, |
| 5076 | 5331 | } |
| ... | ... | @@ -5620,7 +5875,7 @@ pub const Type = extern union { |
| 5620 | 5875 | d.bit_offset == 0 and d.host_size == 0 and !d.@"allowzero" and !d.@"volatile") |
| 5621 | 5876 | { |
| 5622 | 5877 | if (d.sentinel) |sent| { |
| 5623 | if (!d.mutable and d.pointee_type.eql(Type.u8)) { | |
| 5878 | if (!d.mutable and d.pointee_type.eql(Type.u8, target)) { | |
| 5624 | 5879 | switch (d.size) { |
| 5625 | 5880 | .Slice => { |
| 5626 | 5881 | if (sent.compareWithZero(.eq)) { |
| ... | ... | @@ -5635,7 +5890,7 @@ pub const Type = extern union { |
| 5635 | 5890 | else => {}, |
| 5636 | 5891 | } |
| 5637 | 5892 | } |
| 5638 | } else if (!d.mutable and d.pointee_type.eql(Type.u8)) { | |
| 5893 | } else if (!d.mutable and d.pointee_type.eql(Type.u8, target)) { | |
| 5639 | 5894 | switch (d.size) { |
| 5640 | 5895 | .Slice => return Type.initTag(.const_slice_u8), |
| 5641 | 5896 | .Many => return Type.initTag(.manyptr_const_u8), |
| ... | ... | @@ -5669,10 +5924,11 @@ pub const Type = extern union { |
| 5669 | 5924 | len: u64, |
| 5670 | 5925 | sent: ?Value, |
| 5671 | 5926 | elem_type: Type, |
| 5927 | target: Target, | |
| 5672 | 5928 | ) Allocator.Error!Type { |
| 5673 | if (elem_type.eql(Type.u8)) { | |
| 5929 | if (elem_type.eql(Type.u8, target)) { | |
| 5674 | 5930 | if (sent) |some| { |
| 5675 | if (some.eql(Value.zero, elem_type)) { | |
| 5931 | if (some.eql(Value.zero, elem_type, target)) { | |
| 5676 | 5932 | return Tag.array_u8_sentinel_0.create(arena, len); |
| 5677 | 5933 | } |
| 5678 | 5934 | } else { |
| ... | ... | @@ -5715,6 +5971,25 @@ pub const Type = extern union { |
| 5715 | 5971 | } |
| 5716 | 5972 | } |
| 5717 | 5973 | |
| 5974 | pub fn errorUnion( | |
| 5975 | arena: Allocator, | |
| 5976 | error_set: Type, | |
| 5977 | payload: Type, | |
| 5978 | target: Target, | |
| 5979 | ) Allocator.Error!Type { | |
| 5980 | assert(error_set.zigTypeTag() == .ErrorSet); | |
| 5981 | if (error_set.eql(Type.@"anyerror", target) and | |
| 5982 | payload.eql(Type.void, target)) | |
| 5983 | { | |
| 5984 | return Type.initTag(.anyerror_void_error_union); | |
| 5985 | } | |
| 5986 | ||
| 5987 | return Type.Tag.error_union.create(arena, .{ | |
| 5988 | .error_set = error_set, | |
| 5989 | .payload = payload, | |
| 5990 | }); | |
| 5991 | } | |
| 5992 | ||
| 5718 | 5993 | pub fn smallestUnsignedBits(max: u64) u16 { |
| 5719 | 5994 | if (max == 0) return 0; |
| 5720 | 5995 | const base = std.math.log2(max); |
src/value.zig+307-208| ... | ... | @@ -8,6 +8,8 @@ const Target = std.Target; |
| 8 | 8 | const Allocator = std.mem.Allocator; |
| 9 | 9 | const Module = @import("Module.zig"); |
| 10 | 10 | const Air = @import("Air.zig"); |
| 11 | const TypedValue = @import("TypedValue.zig"); | |
| 12 | const Sema = @import("Sema.zig"); | |
| 11 | 13 | |
| 12 | 14 | /// This is the raw data, with no bookkeeping, no memory awareness, |
| 13 | 15 | /// no de-duplication, and no type system awareness. |
| ... | ... | @@ -175,6 +177,8 @@ pub const Value = extern union { |
| 175 | 177 | /// and refers directly to the air. It will never be referenced by the air itself. |
| 176 | 178 | /// TODO: This is probably a bad encoding, maybe put temp data in the sema instead. |
| 177 | 179 | bound_fn, |
| 180 | /// The ABI alignment of the payload type. | |
| 181 | lazy_align, | |
| 178 | 182 | |
| 179 | 183 | pub const last_no_payload_tag = Tag.empty_array; |
| 180 | 184 | pub const no_payload_count = @enumToInt(last_no_payload_tag) + 1; |
| ... | ... | @@ -283,7 +287,10 @@ pub const Value = extern union { |
| 283 | 287 | |
| 284 | 288 | .enum_field_index => Payload.U32, |
| 285 | 289 | |
| 286 | .ty => Payload.Ty, | |
| 290 | .ty, | |
| 291 | .lazy_align, | |
| 292 | => Payload.Ty, | |
| 293 | ||
| 287 | 294 | .int_type => Payload.IntType, |
| 288 | 295 | .int_u64 => Payload.U64, |
| 289 | 296 | .int_i64 => Payload.I64, |
| ... | ... | @@ -453,7 +460,7 @@ pub const Value = extern union { |
| 453 | 460 | .bound_fn, |
| 454 | 461 | => unreachable, |
| 455 | 462 | |
| 456 | .ty => { | |
| 463 | .ty, .lazy_align => { | |
| 457 | 464 | const payload = self.castTag(.ty).?; |
| 458 | 465 | const new_payload = try arena.create(Payload.Ty); |
| 459 | 466 | new_payload.* = .{ |
| ... | ... | @@ -608,7 +615,7 @@ pub const Value = extern union { |
| 608 | 615 | @compileError("do not use format values directly; use either fmtDebug or fmtValue"); |
| 609 | 616 | } |
| 610 | 617 | |
| 611 | /// TODO this should become a debug dump() function. In order to print values in a meaningful way | |
| 618 | /// This is a debug function. In order to print values in a meaningful way | |
| 612 | 619 | /// we also need access to the type. |
| 613 | 620 | pub fn dump( |
| 614 | 621 | start_val: Value, |
| ... | ... | @@ -699,7 +706,12 @@ pub const Value = extern union { |
| 699 | 706 | .the_only_possible_value => return out_stream.writeAll("(the only possible value)"), |
| 700 | 707 | .bool_true => return out_stream.writeAll("true"), |
| 701 | 708 | .bool_false => return out_stream.writeAll("false"), |
| 702 | .ty => return val.castTag(.ty).?.data.format("", options, out_stream), | |
| 709 | .ty => return val.castTag(.ty).?.data.dump("", options, out_stream), | |
| 710 | .lazy_align => { | |
| 711 | try out_stream.writeAll("@alignOf("); | |
| 712 | try val.castTag(.lazy_align).?.data.dump("", options, out_stream); | |
| 713 | try out_stream.writeAll(")"); | |
| 714 | }, | |
| 703 | 715 | .int_type => { |
| 704 | 716 | const int_type = val.castTag(.int_type).?.data; |
| 705 | 717 | return out_stream.print("{s}{d}", .{ |
| ... | ... | @@ -778,15 +790,16 @@ pub const Value = extern union { |
| 778 | 790 | return .{ .data = val }; |
| 779 | 791 | } |
| 780 | 792 | |
| 781 | const TypedValue = @import("TypedValue.zig"); | |
| 782 | ||
| 783 | pub fn fmtValue(val: Value, ty: Type) std.fmt.Formatter(TypedValue.format) { | |
| 784 | return .{ .data = .{ .ty = ty, .val = val } }; | |
| 793 | pub fn fmtValue(val: Value, ty: Type, target: Target) std.fmt.Formatter(TypedValue.format) { | |
| 794 | return .{ .data = .{ | |
| 795 | .tv = .{ .ty = ty, .val = val }, | |
| 796 | .target = target, | |
| 797 | } }; | |
| 785 | 798 | } |
| 786 | 799 | |
| 787 | 800 | /// Asserts that the value is representable as an array of bytes. |
| 788 | 801 | /// Copies the value into a freshly allocated slice of memory, which is owned by the caller. |
| 789 | pub fn toAllocatedBytes(val: Value, ty: Type, allocator: Allocator) ![]u8 { | |
| 802 | pub fn toAllocatedBytes(val: Value, ty: Type, allocator: Allocator, target: Target) ![]u8 { | |
| 790 | 803 | switch (val.tag()) { |
| 791 | 804 | .bytes => { |
| 792 | 805 | const bytes = val.castTag(.bytes).?.data; |
| ... | ... | @@ -796,7 +809,7 @@ pub const Value = extern union { |
| 796 | 809 | }, |
| 797 | 810 | .enum_literal => return allocator.dupe(u8, val.castTag(.enum_literal).?.data), |
| 798 | 811 | .repeated => { |
| 799 | const byte = @intCast(u8, val.castTag(.repeated).?.data.toUnsignedInt()); | |
| 812 | const byte = @intCast(u8, val.castTag(.repeated).?.data.toUnsignedInt(target)); | |
| 800 | 813 | const result = try allocator.alloc(u8, @intCast(usize, ty.arrayLen())); |
| 801 | 814 | std.mem.set(u8, result, byte); |
| 802 | 815 | return result; |
| ... | ... | @@ -804,23 +817,23 @@ pub const Value = extern union { |
| 804 | 817 | .decl_ref => { |
| 805 | 818 | const decl = val.castTag(.decl_ref).?.data; |
| 806 | 819 | const decl_val = try decl.value(); |
| 807 | return decl_val.toAllocatedBytes(decl.ty, allocator); | |
| 820 | return decl_val.toAllocatedBytes(decl.ty, allocator, target); | |
| 808 | 821 | }, |
| 809 | 822 | .the_only_possible_value => return &[_]u8{}, |
| 810 | 823 | .slice => { |
| 811 | 824 | const slice = val.castTag(.slice).?.data; |
| 812 | return arrayToAllocatedBytes(slice.ptr, slice.len.toUnsignedInt(), allocator); | |
| 825 | return arrayToAllocatedBytes(slice.ptr, slice.len.toUnsignedInt(target), allocator, target); | |
| 813 | 826 | }, |
| 814 | else => return arrayToAllocatedBytes(val, ty.arrayLen(), allocator), | |
| 827 | else => return arrayToAllocatedBytes(val, ty.arrayLen(), allocator, target), | |
| 815 | 828 | } |
| 816 | 829 | } |
| 817 | 830 | |
| 818 | fn arrayToAllocatedBytes(val: Value, len: u64, allocator: Allocator) ![]u8 { | |
| 831 | fn arrayToAllocatedBytes(val: Value, len: u64, allocator: Allocator, target: Target) ![]u8 { | |
| 819 | 832 | const result = try allocator.alloc(u8, @intCast(usize, len)); |
| 820 | 833 | var elem_value_buf: ElemValueBuffer = undefined; |
| 821 | 834 | for (result) |*elem, i| { |
| 822 | 835 | const elem_val = val.elemValueBuffer(i, &elem_value_buf); |
| 823 | elem.* = @intCast(u8, elem_val.toUnsignedInt()); | |
| 836 | elem.* = @intCast(u8, elem_val.toUnsignedInt(target)); | |
| 824 | 837 | } |
| 825 | 838 | return result; |
| 826 | 839 | } |
| ... | ... | @@ -977,8 +990,18 @@ pub const Value = extern union { |
| 977 | 990 | } |
| 978 | 991 | |
| 979 | 992 | /// Asserts the value is an integer. |
| 980 | pub fn toBigInt(self: Value, space: *BigIntSpace) BigIntConst { | |
| 981 | switch (self.tag()) { | |
| 993 | pub fn toBigInt(val: Value, space: *BigIntSpace, target: Target) BigIntConst { | |
| 994 | return val.toBigIntAdvanced(space, target, null) catch unreachable; | |
| 995 | } | |
| 996 | ||
| 997 | /// Asserts the value is an integer. | |
| 998 | pub fn toBigIntAdvanced( | |
| 999 | val: Value, | |
| 1000 | space: *BigIntSpace, | |
| 1001 | target: Target, | |
| 1002 | sema_kit: ?Module.WipAnalysis, | |
| 1003 | ) !BigIntConst { | |
| 1004 | switch (val.tag()) { | |
| 982 | 1005 | .zero, |
| 983 | 1006 | .bool_false, |
| 984 | 1007 | .the_only_possible_value, // i0, u0 |
| ... | ... | @@ -988,19 +1011,35 @@ pub const Value = extern union { |
| 988 | 1011 | .bool_true, |
| 989 | 1012 | => return BigIntMutable.init(&space.limbs, 1).toConst(), |
| 990 | 1013 | |
| 991 | .int_u64 => return BigIntMutable.init(&space.limbs, self.castTag(.int_u64).?.data).toConst(), | |
| 992 | .int_i64 => return BigIntMutable.init(&space.limbs, self.castTag(.int_i64).?.data).toConst(), | |
| 993 | .int_big_positive => return self.castTag(.int_big_positive).?.asBigInt(), | |
| 994 | .int_big_negative => return self.castTag(.int_big_negative).?.asBigInt(), | |
| 1014 | .int_u64 => return BigIntMutable.init(&space.limbs, val.castTag(.int_u64).?.data).toConst(), | |
| 1015 | .int_i64 => return BigIntMutable.init(&space.limbs, val.castTag(.int_i64).?.data).toConst(), | |
| 1016 | .int_big_positive => return val.castTag(.int_big_positive).?.asBigInt(), | |
| 1017 | .int_big_negative => return val.castTag(.int_big_negative).?.asBigInt(), | |
| 995 | 1018 | |
| 996 | 1019 | .undef => unreachable, |
| 1020 | ||
| 1021 | .lazy_align => { | |
| 1022 | const ty = val.castTag(.lazy_align).?.data; | |
| 1023 | if (sema_kit) |sk| { | |
| 1024 | try sk.sema.resolveTypeLayout(sk.block, sk.src, ty); | |
| 1025 | } | |
| 1026 | const x = ty.abiAlignment(target); | |
| 1027 | return BigIntMutable.init(&space.limbs, x).toConst(); | |
| 1028 | }, | |
| 1029 | ||
| 997 | 1030 | else => unreachable, |
| 998 | 1031 | } |
| 999 | 1032 | } |
| 1000 | 1033 | |
| 1001 | 1034 | /// If the value fits in a u64, return it, otherwise null. |
| 1002 | 1035 | /// Asserts not undefined. |
| 1003 | pub fn getUnsignedInt(val: Value) ?u64 { | |
| 1036 | pub fn getUnsignedInt(val: Value, target: Target) ?u64 { | |
| 1037 | return getUnsignedIntAdvanced(val, target, null) catch unreachable; | |
| 1038 | } | |
| 1039 | ||
| 1040 | /// If the value fits in a u64, return it, otherwise null. | |
| 1041 | /// Asserts not undefined. | |
| 1042 | pub fn getUnsignedIntAdvanced(val: Value, target: Target, sema_kit: ?Module.WipAnalysis) !?u64 { | |
| 1004 | 1043 | switch (val.tag()) { |
| 1005 | 1044 | .zero, |
| 1006 | 1045 | .bool_false, |
| ... | ... | @@ -1017,13 +1056,22 @@ pub const Value = extern union { |
| 1017 | 1056 | .int_big_negative => return val.castTag(.int_big_negative).?.asBigInt().to(u64) catch null, |
| 1018 | 1057 | |
| 1019 | 1058 | .undef => unreachable, |
| 1059 | ||
| 1060 | .lazy_align => { | |
| 1061 | const ty = val.castTag(.lazy_align).?.data; | |
| 1062 | if (sema_kit) |sk| { | |
| 1063 | try sk.sema.resolveTypeLayout(sk.block, sk.src, ty); | |
| 1064 | } | |
| 1065 | return ty.abiAlignment(target); | |
| 1066 | }, | |
| 1067 | ||
| 1020 | 1068 | else => return null, |
| 1021 | 1069 | } |
| 1022 | 1070 | } |
| 1023 | 1071 | |
| 1024 | 1072 | /// Asserts the value is an integer and it fits in a u64 |
| 1025 | pub fn toUnsignedInt(val: Value) u64 { | |
| 1026 | return getUnsignedInt(val).?; | |
| 1073 | pub fn toUnsignedInt(val: Value, target: Target) u64 { | |
| 1074 | return getUnsignedInt(val, target).?; | |
| 1027 | 1075 | } |
| 1028 | 1076 | |
| 1029 | 1077 | /// Asserts the value is an integer and it fits in a i64 |
| ... | ... | @@ -1066,7 +1114,7 @@ pub const Value = extern union { |
| 1066 | 1114 | switch (ty.zigTypeTag()) { |
| 1067 | 1115 | .Int => { |
| 1068 | 1116 | var bigint_buffer: BigIntSpace = undefined; |
| 1069 | const bigint = val.toBigInt(&bigint_buffer); | |
| 1117 | const bigint = val.toBigInt(&bigint_buffer, target); | |
| 1070 | 1118 | const bits = ty.intInfo(target).bits; |
| 1071 | 1119 | const abi_size = @intCast(usize, ty.abiSize(target)); |
| 1072 | 1120 | bigint.writeTwosComplement(buffer, bits, abi_size, target.cpu.arch.endian()); |
| ... | ... | @@ -1075,7 +1123,7 @@ pub const Value = extern union { |
| 1075 | 1123 | var enum_buffer: Payload.U64 = undefined; |
| 1076 | 1124 | const int_val = val.enumToInt(ty, &enum_buffer); |
| 1077 | 1125 | var bigint_buffer: BigIntSpace = undefined; |
| 1078 | const bigint = int_val.toBigInt(&bigint_buffer); | |
| 1126 | const bigint = int_val.toBigInt(&bigint_buffer, target); | |
| 1079 | 1127 | const bits = ty.intInfo(target).bits; |
| 1080 | 1128 | const abi_size = @intCast(usize, ty.abiSize(target)); |
| 1081 | 1129 | bigint.writeTwosComplement(buffer, bits, abi_size, target.cpu.arch.endian()); |
| ... | ... | @@ -1151,7 +1199,7 @@ pub const Value = extern union { |
| 1151 | 1199 | 128 => bitcastFloatToBigInt(f128, val.toFloat(f128), &field_buf), |
| 1152 | 1200 | else => unreachable, |
| 1153 | 1201 | }, |
| 1154 | .Int, .Bool => field_val.toBigInt(&field_space), | |
| 1202 | .Int, .Bool => field_val.toBigInt(&field_space, target), | |
| 1155 | 1203 | .Struct => packedStructToInt(field_val, field.ty, target, &field_buf), |
| 1156 | 1204 | else => unreachable, |
| 1157 | 1205 | }; |
| ... | ... | @@ -1511,7 +1559,7 @@ pub const Value = extern union { |
| 1511 | 1559 | const info = ty.intInfo(target); |
| 1512 | 1560 | |
| 1513 | 1561 | var buffer: Value.BigIntSpace = undefined; |
| 1514 | const operand_bigint = val.toBigInt(&buffer); | |
| 1562 | const operand_bigint = val.toBigInt(&buffer, target); | |
| 1515 | 1563 | |
| 1516 | 1564 | var limbs_buffer: [4]std.math.big.Limb = undefined; |
| 1517 | 1565 | var result_bigint = BigIntMutable{ |
| ... | ... | @@ -1532,7 +1580,7 @@ pub const Value = extern union { |
| 1532 | 1580 | const info = ty.intInfo(target); |
| 1533 | 1581 | |
| 1534 | 1582 | var buffer: Value.BigIntSpace = undefined; |
| 1535 | const operand_bigint = val.toBigInt(&buffer); | |
| 1583 | const operand_bigint = val.toBigInt(&buffer, target); | |
| 1536 | 1584 | |
| 1537 | 1585 | const limbs = try arena.alloc( |
| 1538 | 1586 | std.math.big.Limb, |
| ... | ... | @@ -1553,7 +1601,7 @@ pub const Value = extern union { |
| 1553 | 1601 | assert(info.bits % 8 == 0); |
| 1554 | 1602 | |
| 1555 | 1603 | var buffer: Value.BigIntSpace = undefined; |
| 1556 | const operand_bigint = val.toBigInt(&buffer); | |
| 1604 | const operand_bigint = val.toBigInt(&buffer, target); | |
| 1557 | 1605 | |
| 1558 | 1606 | const limbs = try arena.alloc( |
| 1559 | 1607 | std.math.big.Limb, |
| ... | ... | @@ -1597,7 +1645,7 @@ pub const Value = extern union { |
| 1597 | 1645 | |
| 1598 | 1646 | else => { |
| 1599 | 1647 | var buffer: BigIntSpace = undefined; |
| 1600 | return self.toBigInt(&buffer).bitCountTwosComp(); | |
| 1648 | return self.toBigInt(&buffer, target).bitCountTwosComp(); | |
| 1601 | 1649 | }, |
| 1602 | 1650 | } |
| 1603 | 1651 | } |
| ... | ... | @@ -1624,6 +1672,17 @@ pub const Value = extern union { |
| 1624 | 1672 | else => unreachable, |
| 1625 | 1673 | }, |
| 1626 | 1674 | |
| 1675 | .lazy_align => { | |
| 1676 | const info = ty.intInfo(target); | |
| 1677 | const max_needed_bits = @as(u16, 16) + @boolToInt(info.signedness == .signed); | |
| 1678 | // If it is u16 or bigger we know the alignment fits without resolving it. | |
| 1679 | if (info.bits >= max_needed_bits) return true; | |
| 1680 | const x = self.castTag(.lazy_align).?.data.abiAlignment(target); | |
| 1681 | if (x == 0) return true; | |
| 1682 | const actual_needed_bits = std.math.log2(x) + 1 + @boolToInt(info.signedness == .signed); | |
| 1683 | return info.bits >= actual_needed_bits; | |
| 1684 | }, | |
| 1685 | ||
| 1627 | 1686 | .int_u64 => switch (ty.zigTypeTag()) { |
| 1628 | 1687 | .Int => { |
| 1629 | 1688 | const x = self.castTag(.int_u64).?.data; |
| ... | ... | @@ -1643,7 +1702,7 @@ pub const Value = extern union { |
| 1643 | 1702 | if (info.signedness == .unsigned and x < 0) |
| 1644 | 1703 | return false; |
| 1645 | 1704 | var buffer: BigIntSpace = undefined; |
| 1646 | return self.toBigInt(&buffer).fitsInTwosComp(info.signedness, info.bits); | |
| 1705 | return self.toBigInt(&buffer, target).fitsInTwosComp(info.signedness, info.bits); | |
| 1647 | 1706 | }, |
| 1648 | 1707 | .ComptimeInt => return true, |
| 1649 | 1708 | else => unreachable, |
| ... | ... | @@ -1745,6 +1804,10 @@ pub const Value = extern union { |
| 1745 | 1804 | } |
| 1746 | 1805 | |
| 1747 | 1806 | pub fn orderAgainstZero(lhs: Value) std.math.Order { |
| 1807 | return orderAgainstZeroAdvanced(lhs, null) catch unreachable; | |
| 1808 | } | |
| 1809 | ||
| 1810 | pub fn orderAgainstZeroAdvanced(lhs: Value, sema_kit: ?Module.WipAnalysis) !std.math.Order { | |
| 1748 | 1811 | return switch (lhs.tag()) { |
| 1749 | 1812 | .zero, |
| 1750 | 1813 | .bool_false, |
| ... | ... | @@ -1765,6 +1828,15 @@ pub const Value = extern union { |
| 1765 | 1828 | .int_big_positive => lhs.castTag(.int_big_positive).?.asBigInt().orderAgainstScalar(0), |
| 1766 | 1829 | .int_big_negative => lhs.castTag(.int_big_negative).?.asBigInt().orderAgainstScalar(0), |
| 1767 | 1830 | |
| 1831 | .lazy_align => { | |
| 1832 | const ty = lhs.castTag(.lazy_align).?.data; | |
| 1833 | if (try ty.hasRuntimeBitsAdvanced(false, sema_kit)) { | |
| 1834 | return .gt; | |
| 1835 | } else { | |
| 1836 | return .eq; | |
| 1837 | } | |
| 1838 | }, | |
| 1839 | ||
| 1768 | 1840 | .float_16 => std.math.order(lhs.castTag(.float_16).?.data, 0), |
| 1769 | 1841 | .float_32 => std.math.order(lhs.castTag(.float_32).?.data, 0), |
| 1770 | 1842 | .float_64 => std.math.order(lhs.castTag(.float_64).?.data, 0), |
| ... | ... | @@ -1776,11 +1848,17 @@ pub const Value = extern union { |
| 1776 | 1848 | } |
| 1777 | 1849 | |
| 1778 | 1850 | /// Asserts the value is comparable. |
| 1779 | pub fn order(lhs: Value, rhs: Value) std.math.Order { | |
| 1851 | pub fn order(lhs: Value, rhs: Value, target: Target) std.math.Order { | |
| 1852 | return orderAdvanced(lhs, rhs, target, null) catch unreachable; | |
| 1853 | } | |
| 1854 | ||
| 1855 | /// Asserts the value is comparable. | |
| 1856 | /// If sema_kit is null then this function asserts things are resolved and cannot fail. | |
| 1857 | pub fn orderAdvanced(lhs: Value, rhs: Value, target: Target, sema_kit: ?Module.WipAnalysis) !std.math.Order { | |
| 1780 | 1858 | const lhs_tag = lhs.tag(); |
| 1781 | 1859 | const rhs_tag = rhs.tag(); |
| 1782 | const lhs_against_zero = lhs.orderAgainstZero(); | |
| 1783 | const rhs_against_zero = rhs.orderAgainstZero(); | |
| 1860 | const lhs_against_zero = try lhs.orderAgainstZeroAdvanced(sema_kit); | |
| 1861 | const rhs_against_zero = try rhs.orderAgainstZeroAdvanced(sema_kit); | |
| 1784 | 1862 | switch (lhs_against_zero) { |
| 1785 | 1863 | .lt => if (rhs_against_zero != .lt) return .lt, |
| 1786 | 1864 | .eq => return rhs_against_zero.invert(), |
| ... | ... | @@ -1814,14 +1892,24 @@ pub const Value = extern union { |
| 1814 | 1892 | |
| 1815 | 1893 | var lhs_bigint_space: BigIntSpace = undefined; |
| 1816 | 1894 | var rhs_bigint_space: BigIntSpace = undefined; |
| 1817 | const lhs_bigint = lhs.toBigInt(&lhs_bigint_space); | |
| 1818 | const rhs_bigint = rhs.toBigInt(&rhs_bigint_space); | |
| 1895 | const lhs_bigint = try lhs.toBigIntAdvanced(&lhs_bigint_space, target, sema_kit); | |
| 1896 | const rhs_bigint = try rhs.toBigIntAdvanced(&rhs_bigint_space, target, sema_kit); | |
| 1819 | 1897 | return lhs_bigint.order(rhs_bigint); |
| 1820 | 1898 | } |
| 1821 | 1899 | |
| 1822 | 1900 | /// Asserts the value is comparable. Does not take a type parameter because it supports |
| 1823 | 1901 | /// comparisons between heterogeneous types. |
| 1824 | pub fn compareHetero(lhs: Value, op: std.math.CompareOperator, rhs: Value) bool { | |
| 1902 | pub fn compareHetero(lhs: Value, op: std.math.CompareOperator, rhs: Value, target: Target) bool { | |
| 1903 | return compareHeteroAdvanced(lhs, op, rhs, target, null) catch unreachable; | |
| 1904 | } | |
| 1905 | ||
| 1906 | pub fn compareHeteroAdvanced( | |
| 1907 | lhs: Value, | |
| 1908 | op: std.math.CompareOperator, | |
| 1909 | rhs: Value, | |
| 1910 | target: Target, | |
| 1911 | sema_kit: ?Module.WipAnalysis, | |
| 1912 | ) !bool { | |
| 1825 | 1913 | if (lhs.pointerDecl()) |lhs_decl| { |
| 1826 | 1914 | if (rhs.pointerDecl()) |rhs_decl| { |
| 1827 | 1915 | switch (op) { |
| ... | ... | @@ -1843,39 +1931,39 @@ pub const Value = extern union { |
| 1843 | 1931 | else => {}, |
| 1844 | 1932 | } |
| 1845 | 1933 | } |
| 1846 | return order(lhs, rhs).compare(op); | |
| 1934 | return (try orderAdvanced(lhs, rhs, target, sema_kit)).compare(op); | |
| 1847 | 1935 | } |
| 1848 | 1936 | |
| 1849 | 1937 | /// Asserts the values are comparable. Both operands have type `ty`. |
| 1850 | 1938 | /// Vector results will be reduced with AND. |
| 1851 | pub fn compare(lhs: Value, op: std.math.CompareOperator, rhs: Value, ty: Type) bool { | |
| 1939 | pub fn compare(lhs: Value, op: std.math.CompareOperator, rhs: Value, ty: Type, target: Target) bool { | |
| 1852 | 1940 | if (ty.zigTypeTag() == .Vector) { |
| 1853 | 1941 | var i: usize = 0; |
| 1854 | 1942 | while (i < ty.vectorLen()) : (i += 1) { |
| 1855 | if (!compareScalar(lhs.indexVectorlike(i), op, rhs.indexVectorlike(i), ty.scalarType())) { | |
| 1943 | if (!compareScalar(lhs.indexVectorlike(i), op, rhs.indexVectorlike(i), ty.scalarType(), target)) { | |
| 1856 | 1944 | return false; |
| 1857 | 1945 | } |
| 1858 | 1946 | } |
| 1859 | 1947 | return true; |
| 1860 | 1948 | } |
| 1861 | return compareScalar(lhs, op, rhs, ty); | |
| 1949 | return compareScalar(lhs, op, rhs, ty, target); | |
| 1862 | 1950 | } |
| 1863 | 1951 | |
| 1864 | 1952 | /// Asserts the values are comparable. Both operands have type `ty`. |
| 1865 | pub fn compareScalar(lhs: Value, op: std.math.CompareOperator, rhs: Value, ty: Type) bool { | |
| 1953 | pub fn compareScalar(lhs: Value, op: std.math.CompareOperator, rhs: Value, ty: Type, target: Target) bool { | |
| 1866 | 1954 | return switch (op) { |
| 1867 | .eq => lhs.eql(rhs, ty), | |
| 1868 | .neq => !lhs.eql(rhs, ty), | |
| 1869 | else => compareHetero(lhs, op, rhs), | |
| 1955 | .eq => lhs.eql(rhs, ty, target), | |
| 1956 | .neq => !lhs.eql(rhs, ty, target), | |
| 1957 | else => compareHetero(lhs, op, rhs, target), | |
| 1870 | 1958 | }; |
| 1871 | 1959 | } |
| 1872 | 1960 | |
| 1873 | 1961 | /// Asserts the values are comparable vectors of type `ty`. |
| 1874 | pub fn compareVector(lhs: Value, op: std.math.CompareOperator, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 1962 | pub fn compareVector(lhs: Value, op: std.math.CompareOperator, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 1875 | 1963 | assert(ty.zigTypeTag() == .Vector); |
| 1876 | 1964 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 1877 | 1965 | for (result_data) |*scalar, i| { |
| 1878 | const res_bool = compareScalar(lhs.indexVectorlike(i), op, rhs.indexVectorlike(i), ty.scalarType()); | |
| 1966 | const res_bool = compareScalar(lhs.indexVectorlike(i), op, rhs.indexVectorlike(i), ty.scalarType(), target); | |
| 1879 | 1967 | scalar.* = if (res_bool) Value.@"true" else Value.@"false"; |
| 1880 | 1968 | } |
| 1881 | 1969 | return Value.Tag.aggregate.create(allocator, result_data); |
| ... | ... | @@ -1899,12 +1987,12 @@ pub const Value = extern union { |
| 1899 | 1987 | |
| 1900 | 1988 | /// This function is used by hash maps and so treats floating-point NaNs as equal |
| 1901 | 1989 | /// to each other, and not equal to other floating-point values. |
| 1902 | pub fn eql(a: Value, b: Value, ty: Type) bool { | |
| 1990 | /// Similarly, it treats `undef` as a distinct value from all other values. | |
| 1991 | pub fn eql(a: Value, b: Value, ty: Type, target: Target) bool { | |
| 1903 | 1992 | const a_tag = a.tag(); |
| 1904 | 1993 | const b_tag = b.tag(); |
| 1905 | assert(a_tag != .undef); | |
| 1906 | assert(b_tag != .undef); | |
| 1907 | 1994 | if (a_tag == b_tag) switch (a_tag) { |
| 1995 | .undef => return true, | |
| 1908 | 1996 | .void_value, .null_value, .the_only_possible_value, .empty_struct_value => return true, |
| 1909 | 1997 | .enum_literal => { |
| 1910 | 1998 | const a_name = a.castTag(.enum_literal).?.data; |
| ... | ... | @@ -1920,31 +2008,31 @@ pub const Value = extern union { |
| 1920 | 2008 | const a_payload = a.castTag(.opt_payload).?.data; |
| 1921 | 2009 | const b_payload = b.castTag(.opt_payload).?.data; |
| 1922 | 2010 | var buffer: Type.Payload.ElemType = undefined; |
| 1923 | return eql(a_payload, b_payload, ty.optionalChild(&buffer)); | |
| 2011 | return eql(a_payload, b_payload, ty.optionalChild(&buffer), target); | |
| 1924 | 2012 | }, |
| 1925 | 2013 | .slice => { |
| 1926 | 2014 | const a_payload = a.castTag(.slice).?.data; |
| 1927 | 2015 | const b_payload = b.castTag(.slice).?.data; |
| 1928 | if (!eql(a_payload.len, b_payload.len, Type.usize)) return false; | |
| 2016 | if (!eql(a_payload.len, b_payload.len, Type.usize, target)) return false; | |
| 1929 | 2017 | |
| 1930 | 2018 | var ptr_buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 1931 | 2019 | const ptr_ty = ty.slicePtrFieldType(&ptr_buf); |
| 1932 | 2020 | |
| 1933 | return eql(a_payload.ptr, b_payload.ptr, ptr_ty); | |
| 2021 | return eql(a_payload.ptr, b_payload.ptr, ptr_ty, target); | |
| 1934 | 2022 | }, |
| 1935 | 2023 | .elem_ptr => { |
| 1936 | 2024 | const a_payload = a.castTag(.elem_ptr).?.data; |
| 1937 | 2025 | const b_payload = b.castTag(.elem_ptr).?.data; |
| 1938 | 2026 | if (a_payload.index != b_payload.index) return false; |
| 1939 | 2027 | |
| 1940 | return eql(a_payload.array_ptr, b_payload.array_ptr, ty); | |
| 2028 | return eql(a_payload.array_ptr, b_payload.array_ptr, ty, target); | |
| 1941 | 2029 | }, |
| 1942 | 2030 | .field_ptr => { |
| 1943 | 2031 | const a_payload = a.castTag(.field_ptr).?.data; |
| 1944 | 2032 | const b_payload = b.castTag(.field_ptr).?.data; |
| 1945 | 2033 | if (a_payload.field_index != b_payload.field_index) return false; |
| 1946 | 2034 | |
| 1947 | return eql(a_payload.container_ptr, b_payload.container_ptr, ty); | |
| 2035 | return eql(a_payload.container_ptr, b_payload.container_ptr, ty, target); | |
| 1948 | 2036 | }, |
| 1949 | 2037 | .@"error" => { |
| 1950 | 2038 | const a_name = a.castTag(.@"error").?.data.name; |
| ... | ... | @@ -1954,7 +2042,7 @@ pub const Value = extern union { |
| 1954 | 2042 | .eu_payload => { |
| 1955 | 2043 | const a_payload = a.castTag(.eu_payload).?.data; |
| 1956 | 2044 | const b_payload = b.castTag(.eu_payload).?.data; |
| 1957 | return eql(a_payload, b_payload, ty.errorUnionPayload()); | |
| 2045 | return eql(a_payload, b_payload, ty.errorUnionPayload(), target); | |
| 1958 | 2046 | }, |
| 1959 | 2047 | .eu_payload_ptr => @panic("TODO: Implement more pointer eql cases"), |
| 1960 | 2048 | .opt_payload_ptr => @panic("TODO: Implement more pointer eql cases"), |
| ... | ... | @@ -1972,7 +2060,7 @@ pub const Value = extern union { |
| 1972 | 2060 | const types = ty.tupleFields().types; |
| 1973 | 2061 | assert(types.len == a_field_vals.len); |
| 1974 | 2062 | for (types) |field_ty, i| { |
| 1975 | if (!eql(a_field_vals[i], b_field_vals[i], field_ty)) return false; | |
| 2063 | if (!eql(a_field_vals[i], b_field_vals[i], field_ty, target)) return false; | |
| 1976 | 2064 | } |
| 1977 | 2065 | return true; |
| 1978 | 2066 | } |
| ... | ... | @@ -1981,7 +2069,7 @@ pub const Value = extern union { |
| 1981 | 2069 | const fields = ty.structFields().values(); |
| 1982 | 2070 | assert(fields.len == a_field_vals.len); |
| 1983 | 2071 | for (fields) |field, i| { |
| 1984 | if (!eql(a_field_vals[i], b_field_vals[i], field.ty)) return false; | |
| 2072 | if (!eql(a_field_vals[i], b_field_vals[i], field.ty, target)) return false; | |
| 1985 | 2073 | } |
| 1986 | 2074 | return true; |
| 1987 | 2075 | } |
| ... | ... | @@ -1990,7 +2078,7 @@ pub const Value = extern union { |
| 1990 | 2078 | for (a_field_vals) |a_elem, i| { |
| 1991 | 2079 | const b_elem = b_field_vals[i]; |
| 1992 | 2080 | |
| 1993 | if (!eql(a_elem, b_elem, elem_ty)) return false; | |
| 2081 | if (!eql(a_elem, b_elem, elem_ty, target)) return false; | |
| 1994 | 2082 | } |
| 1995 | 2083 | return true; |
| 1996 | 2084 | }, |
| ... | ... | @@ -2005,17 +2093,19 @@ pub const Value = extern union { |
| 2005 | 2093 | }, |
| 2006 | 2094 | .Auto => { |
| 2007 | 2095 | const tag_ty = ty.unionTagTypeHypothetical(); |
| 2008 | if (!a_union.tag.eql(b_union.tag, tag_ty)) { | |
| 2096 | if (!a_union.tag.eql(b_union.tag, tag_ty, target)) { | |
| 2009 | 2097 | return false; |
| 2010 | 2098 | } |
| 2011 | const active_field_ty = ty.unionFieldType(a_union.tag); | |
| 2012 | return a_union.val.eql(b_union.val, active_field_ty); | |
| 2099 | const active_field_ty = ty.unionFieldType(a_union.tag, target); | |
| 2100 | return a_union.val.eql(b_union.val, active_field_ty, target); | |
| 2013 | 2101 | }, |
| 2014 | 2102 | } |
| 2015 | 2103 | }, |
| 2016 | 2104 | else => {}, |
| 2017 | 2105 | } else if (a_tag == .null_value or b_tag == .null_value) { |
| 2018 | 2106 | return false; |
| 2107 | } else if (a_tag == .undef or b_tag == .undef) { | |
| 2108 | return false; | |
| 2019 | 2109 | } |
| 2020 | 2110 | |
| 2021 | 2111 | if (a.pointerDecl()) |a_decl| { |
| ... | ... | @@ -2034,7 +2124,7 @@ pub const Value = extern union { |
| 2034 | 2124 | var buf_b: ToTypeBuffer = undefined; |
| 2035 | 2125 | const a_type = a.toType(&buf_a); |
| 2036 | 2126 | const b_type = b.toType(&buf_b); |
| 2037 | return a_type.eql(b_type); | |
| 2127 | return a_type.eql(b_type, target); | |
| 2038 | 2128 | }, |
| 2039 | 2129 | .Enum => { |
| 2040 | 2130 | var buf_a: Payload.U64 = undefined; |
| ... | ... | @@ -2043,7 +2133,7 @@ pub const Value = extern union { |
| 2043 | 2133 | const b_val = b.enumToInt(ty, &buf_b); |
| 2044 | 2134 | var buf_ty: Type.Payload.Bits = undefined; |
| 2045 | 2135 | const int_ty = ty.intTagType(&buf_ty); |
| 2046 | return eql(a_val, b_val, int_ty); | |
| 2136 | return eql(a_val, b_val, int_ty, target); | |
| 2047 | 2137 | }, |
| 2048 | 2138 | .Array, .Vector => { |
| 2049 | 2139 | const len = ty.arrayLen(); |
| ... | ... | @@ -2054,7 +2144,7 @@ pub const Value = extern union { |
| 2054 | 2144 | while (i < len) : (i += 1) { |
| 2055 | 2145 | const a_elem = elemValueBuffer(a, i, &a_buf); |
| 2056 | 2146 | const b_elem = elemValueBuffer(b, i, &b_buf); |
| 2057 | if (!eql(a_elem, b_elem, elem_ty)) return false; | |
| 2147 | if (!eql(a_elem, b_elem, elem_ty, target)) return false; | |
| 2058 | 2148 | } |
| 2059 | 2149 | return true; |
| 2060 | 2150 | }, |
| ... | ... | @@ -2070,15 +2160,15 @@ pub const Value = extern union { |
| 2070 | 2160 | if (a_nan or b_nan) { |
| 2071 | 2161 | return a_nan and b_nan; |
| 2072 | 2162 | } |
| 2073 | return order(a, b).compare(.eq); | |
| 2163 | return order(a, b, target).compare(.eq); | |
| 2074 | 2164 | }, |
| 2075 | else => return order(a, b).compare(.eq), | |
| 2165 | else => return order(a, b, target).compare(.eq), | |
| 2076 | 2166 | } |
| 2077 | 2167 | } |
| 2078 | 2168 | |
| 2079 | 2169 | /// This function is used by hash maps and so treats floating-point NaNs as equal |
| 2080 | 2170 | /// to each other, and not equal to other floating-point values. |
| 2081 | pub fn hash(val: Value, ty: Type, hasher: *std.hash.Wyhash) void { | |
| 2171 | pub fn hash(val: Value, ty: Type, hasher: *std.hash.Wyhash, target: Target) void { | |
| 2082 | 2172 | const zig_ty_tag = ty.zigTypeTag(); |
| 2083 | 2173 | std.hash.autoHash(hasher, zig_ty_tag); |
| 2084 | 2174 | if (val.isUndef()) return; |
| ... | ... | @@ -2095,7 +2185,7 @@ pub const Value = extern union { |
| 2095 | 2185 | |
| 2096 | 2186 | .Type => { |
| 2097 | 2187 | var buf: ToTypeBuffer = undefined; |
| 2098 | return val.toType(&buf).hashWithHasher(hasher); | |
| 2188 | return val.toType(&buf).hashWithHasher(hasher, target); | |
| 2099 | 2189 | }, |
| 2100 | 2190 | .Float, .ComptimeFloat => { |
| 2101 | 2191 | // Normalize the float here because this hash must match eql semantics. |
| ... | ... | @@ -2116,11 +2206,11 @@ pub const Value = extern union { |
| 2116 | 2206 | const slice = val.castTag(.slice).?.data; |
| 2117 | 2207 | var ptr_buf: Type.SlicePtrFieldTypeBuffer = undefined; |
| 2118 | 2208 | const ptr_ty = ty.slicePtrFieldType(&ptr_buf); |
| 2119 | hash(slice.ptr, ptr_ty, hasher); | |
| 2120 | hash(slice.len, Type.usize, hasher); | |
| 2209 | hash(slice.ptr, ptr_ty, hasher, target); | |
| 2210 | hash(slice.len, Type.usize, hasher, target); | |
| 2121 | 2211 | }, |
| 2122 | 2212 | |
| 2123 | else => return hashPtr(val, hasher), | |
| 2213 | else => return hashPtr(val, hasher, target), | |
| 2124 | 2214 | }, |
| 2125 | 2215 | .Array, .Vector => { |
| 2126 | 2216 | const len = ty.arrayLen(); |
| ... | ... | @@ -2129,14 +2219,14 @@ pub const Value = extern union { |
| 2129 | 2219 | var elem_value_buf: ElemValueBuffer = undefined; |
| 2130 | 2220 | while (index < len) : (index += 1) { |
| 2131 | 2221 | const elem_val = val.elemValueBuffer(index, &elem_value_buf); |
| 2132 | elem_val.hash(elem_ty, hasher); | |
| 2222 | elem_val.hash(elem_ty, hasher, target); | |
| 2133 | 2223 | } |
| 2134 | 2224 | }, |
| 2135 | 2225 | .Struct => { |
| 2136 | 2226 | if (ty.isTupleOrAnonStruct()) { |
| 2137 | 2227 | const fields = ty.tupleFields(); |
| 2138 | 2228 | for (fields.values) |field_val, i| { |
| 2139 | field_val.hash(fields.types[i], hasher); | |
| 2229 | field_val.hash(fields.types[i], hasher, target); | |
| 2140 | 2230 | } |
| 2141 | 2231 | return; |
| 2142 | 2232 | } |
| ... | ... | @@ -2145,13 +2235,13 @@ pub const Value = extern union { |
| 2145 | 2235 | switch (val.tag()) { |
| 2146 | 2236 | .empty_struct_value => { |
| 2147 | 2237 | for (fields) |field| { |
| 2148 | field.default_val.hash(field.ty, hasher); | |
| 2238 | field.default_val.hash(field.ty, hasher, target); | |
| 2149 | 2239 | } |
| 2150 | 2240 | }, |
| 2151 | 2241 | .aggregate => { |
| 2152 | 2242 | const field_values = val.castTag(.aggregate).?.data; |
| 2153 | 2243 | for (field_values) |field_val, i| { |
| 2154 | field_val.hash(fields[i].ty, hasher); | |
| 2244 | field_val.hash(fields[i].ty, hasher, target); | |
| 2155 | 2245 | } |
| 2156 | 2246 | }, |
| 2157 | 2247 | else => unreachable, |
| ... | ... | @@ -2163,7 +2253,7 @@ pub const Value = extern union { |
| 2163 | 2253 | const sub_val = payload.data; |
| 2164 | 2254 | var buffer: Type.Payload.ElemType = undefined; |
| 2165 | 2255 | const sub_ty = ty.optionalChild(&buffer); |
| 2166 | sub_val.hash(sub_ty, hasher); | |
| 2256 | sub_val.hash(sub_ty, hasher, target); | |
| 2167 | 2257 | } else { |
| 2168 | 2258 | std.hash.autoHash(hasher, false); // non-null |
| 2169 | 2259 | } |
| ... | ... | @@ -2172,14 +2262,14 @@ pub const Value = extern union { |
| 2172 | 2262 | if (val.tag() == .@"error") { |
| 2173 | 2263 | std.hash.autoHash(hasher, false); // error |
| 2174 | 2264 | const sub_ty = ty.errorUnionSet(); |
| 2175 | val.hash(sub_ty, hasher); | |
| 2265 | val.hash(sub_ty, hasher, target); | |
| 2176 | 2266 | return; |
| 2177 | 2267 | } |
| 2178 | 2268 | |
| 2179 | 2269 | if (val.castTag(.eu_payload)) |payload| { |
| 2180 | 2270 | std.hash.autoHash(hasher, true); // payload |
| 2181 | 2271 | const sub_ty = ty.errorUnionPayload(); |
| 2182 | payload.data.hash(sub_ty, hasher); | |
| 2272 | payload.data.hash(sub_ty, hasher, target); | |
| 2183 | 2273 | return; |
| 2184 | 2274 | } else unreachable; |
| 2185 | 2275 | }, |
| ... | ... | @@ -2192,15 +2282,15 @@ pub const Value = extern union { |
| 2192 | 2282 | .Enum => { |
| 2193 | 2283 | var enum_space: Payload.U64 = undefined; |
| 2194 | 2284 | const int_val = val.enumToInt(ty, &enum_space); |
| 2195 | hashInt(int_val, hasher); | |
| 2285 | hashInt(int_val, hasher, target); | |
| 2196 | 2286 | }, |
| 2197 | 2287 | .Union => { |
| 2198 | 2288 | const union_obj = val.cast(Payload.Union).?.data; |
| 2199 | 2289 | if (ty.unionTagType()) |tag_ty| { |
| 2200 | union_obj.tag.hash(tag_ty, hasher); | |
| 2290 | union_obj.tag.hash(tag_ty, hasher, target); | |
| 2201 | 2291 | } |
| 2202 | const active_field_ty = ty.unionFieldType(union_obj.tag); | |
| 2203 | union_obj.val.hash(active_field_ty, hasher); | |
| 2292 | const active_field_ty = ty.unionFieldType(union_obj.tag, target); | |
| 2293 | union_obj.val.hash(active_field_ty, hasher, target); | |
| 2204 | 2294 | }, |
| 2205 | 2295 | .Fn => { |
| 2206 | 2296 | const func: *Module.Fn = val.castTag(.function).?.data; |
| ... | ... | @@ -2225,28 +2315,30 @@ pub const Value = extern union { |
| 2225 | 2315 | |
| 2226 | 2316 | pub const ArrayHashContext = struct { |
| 2227 | 2317 | ty: Type, |
| 2318 | target: Target, | |
| 2228 | 2319 | |
| 2229 | 2320 | pub fn hash(self: @This(), val: Value) u32 { |
| 2230 | const other_context: HashContext = .{ .ty = self.ty }; | |
| 2321 | const other_context: HashContext = .{ .ty = self.ty, .target = self.target }; | |
| 2231 | 2322 | return @truncate(u32, other_context.hash(val)); |
| 2232 | 2323 | } |
| 2233 | 2324 | pub fn eql(self: @This(), a: Value, b: Value, b_index: usize) bool { |
| 2234 | 2325 | _ = b_index; |
| 2235 | return a.eql(b, self.ty); | |
| 2326 | return a.eql(b, self.ty, self.target); | |
| 2236 | 2327 | } |
| 2237 | 2328 | }; |
| 2238 | 2329 | |
| 2239 | 2330 | pub const HashContext = struct { |
| 2240 | 2331 | ty: Type, |
| 2332 | target: Target, | |
| 2241 | 2333 | |
| 2242 | 2334 | pub fn hash(self: @This(), val: Value) u64 { |
| 2243 | 2335 | var hasher = std.hash.Wyhash.init(0); |
| 2244 | val.hash(self.ty, &hasher); | |
| 2336 | val.hash(self.ty, &hasher, self.target); | |
| 2245 | 2337 | return hasher.final(); |
| 2246 | 2338 | } |
| 2247 | 2339 | |
| 2248 | 2340 | pub fn eql(self: @This(), a: Value, b: Value) bool { |
| 2249 | return a.eql(b, self.ty); | |
| 2341 | return a.eql(b, self.ty, self.target); | |
| 2250 | 2342 | } |
| 2251 | 2343 | }; |
| 2252 | 2344 | |
| ... | ... | @@ -2296,16 +2388,16 @@ pub const Value = extern union { |
| 2296 | 2388 | }; |
| 2297 | 2389 | } |
| 2298 | 2390 | |
| 2299 | fn hashInt(int_val: Value, hasher: *std.hash.Wyhash) void { | |
| 2391 | fn hashInt(int_val: Value, hasher: *std.hash.Wyhash, target: Target) void { | |
| 2300 | 2392 | var buffer: BigIntSpace = undefined; |
| 2301 | const big = int_val.toBigInt(&buffer); | |
| 2393 | const big = int_val.toBigInt(&buffer, target); | |
| 2302 | 2394 | std.hash.autoHash(hasher, big.positive); |
| 2303 | 2395 | for (big.limbs) |limb| { |
| 2304 | 2396 | std.hash.autoHash(hasher, limb); |
| 2305 | 2397 | } |
| 2306 | 2398 | } |
| 2307 | 2399 | |
| 2308 | fn hashPtr(ptr_val: Value, hasher: *std.hash.Wyhash) void { | |
| 2400 | fn hashPtr(ptr_val: Value, hasher: *std.hash.Wyhash, target: Target) void { | |
| 2309 | 2401 | switch (ptr_val.tag()) { |
| 2310 | 2402 | .decl_ref, |
| 2311 | 2403 | .decl_ref_mut, |
| ... | ... | @@ -2319,25 +2411,25 @@ pub const Value = extern union { |
| 2319 | 2411 | |
| 2320 | 2412 | .elem_ptr => { |
| 2321 | 2413 | const elem_ptr = ptr_val.castTag(.elem_ptr).?.data; |
| 2322 | hashPtr(elem_ptr.array_ptr, hasher); | |
| 2414 | hashPtr(elem_ptr.array_ptr, hasher, target); | |
| 2323 | 2415 | std.hash.autoHash(hasher, Value.Tag.elem_ptr); |
| 2324 | 2416 | std.hash.autoHash(hasher, elem_ptr.index); |
| 2325 | 2417 | }, |
| 2326 | 2418 | .field_ptr => { |
| 2327 | 2419 | const field_ptr = ptr_val.castTag(.field_ptr).?.data; |
| 2328 | 2420 | std.hash.autoHash(hasher, Value.Tag.field_ptr); |
| 2329 | hashPtr(field_ptr.container_ptr, hasher); | |
| 2421 | hashPtr(field_ptr.container_ptr, hasher, target); | |
| 2330 | 2422 | std.hash.autoHash(hasher, field_ptr.field_index); |
| 2331 | 2423 | }, |
| 2332 | 2424 | .eu_payload_ptr => { |
| 2333 | 2425 | const err_union_ptr = ptr_val.castTag(.eu_payload_ptr).?.data; |
| 2334 | 2426 | std.hash.autoHash(hasher, Value.Tag.eu_payload_ptr); |
| 2335 | hashPtr(err_union_ptr.container_ptr, hasher); | |
| 2427 | hashPtr(err_union_ptr.container_ptr, hasher, target); | |
| 2336 | 2428 | }, |
| 2337 | 2429 | .opt_payload_ptr => { |
| 2338 | 2430 | const opt_ptr = ptr_val.castTag(.opt_payload_ptr).?.data; |
| 2339 | 2431 | std.hash.autoHash(hasher, Value.Tag.opt_payload_ptr); |
| 2340 | hashPtr(opt_ptr.container_ptr, hasher); | |
| 2432 | hashPtr(opt_ptr.container_ptr, hasher, target); | |
| 2341 | 2433 | }, |
| 2342 | 2434 | |
| 2343 | 2435 | .zero, |
| ... | ... | @@ -2349,7 +2441,7 @@ pub const Value = extern union { |
| 2349 | 2441 | .bool_false, |
| 2350 | 2442 | .bool_true, |
| 2351 | 2443 | .the_only_possible_value, |
| 2352 | => return hashInt(ptr_val, hasher), | |
| 2444 | => return hashInt(ptr_val, hasher, target), | |
| 2353 | 2445 | |
| 2354 | 2446 | else => unreachable, |
| 2355 | 2447 | } |
| ... | ... | @@ -2411,9 +2503,9 @@ pub const Value = extern union { |
| 2411 | 2503 | }; |
| 2412 | 2504 | } |
| 2413 | 2505 | |
| 2414 | pub fn sliceLen(val: Value) u64 { | |
| 2506 | pub fn sliceLen(val: Value, target: Target) u64 { | |
| 2415 | 2507 | return switch (val.tag()) { |
| 2416 | .slice => val.castTag(.slice).?.data.len.toUnsignedInt(), | |
| 2508 | .slice => val.castTag(.slice).?.data.len.toUnsignedInt(target), | |
| 2417 | 2509 | .decl_ref => { |
| 2418 | 2510 | const decl = val.castTag(.decl_ref).?.data; |
| 2419 | 2511 | if (decl.ty.zigTypeTag() == .Array) { |
| ... | ... | @@ -2561,7 +2653,7 @@ pub const Value = extern union { |
| 2561 | 2653 | } |
| 2562 | 2654 | |
| 2563 | 2655 | /// Returns a pointer to the element value at the index. |
| 2564 | pub fn elemPtr(val: Value, ty: Type, arena: Allocator, index: usize) Allocator.Error!Value { | |
| 2656 | pub fn elemPtr(val: Value, ty: Type, arena: Allocator, index: usize, target: Target) Allocator.Error!Value { | |
| 2565 | 2657 | const elem_ty = ty.elemType2(); |
| 2566 | 2658 | const ptr_val = switch (val.tag()) { |
| 2567 | 2659 | .slice => val.castTag(.slice).?.data.ptr, |
| ... | ... | @@ -2570,7 +2662,7 @@ pub const Value = extern union { |
| 2570 | 2662 | |
| 2571 | 2663 | if (ptr_val.tag() == .elem_ptr) { |
| 2572 | 2664 | const elem_ptr = ptr_val.castTag(.elem_ptr).?.data; |
| 2573 | if (elem_ptr.elem_ty.eql(elem_ty)) { | |
| 2665 | if (elem_ptr.elem_ty.eql(elem_ty, target)) { | |
| 2574 | 2666 | return Tag.elem_ptr.create(arena, .{ |
| 2575 | 2667 | .array_ptr = elem_ptr.array_ptr, |
| 2576 | 2668 | .elem_ty = elem_ptr.elem_ty, |
| ... | ... | @@ -2821,8 +2913,8 @@ pub const Value = extern union { |
| 2821 | 2913 | |
| 2822 | 2914 | var lhs_space: Value.BigIntSpace = undefined; |
| 2823 | 2915 | var rhs_space: Value.BigIntSpace = undefined; |
| 2824 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 2825 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 2916 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 2917 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 2826 | 2918 | const limbs = try arena.alloc( |
| 2827 | 2919 | std.math.big.Limb, |
| 2828 | 2920 | std.math.big.int.calcTwosCompLimbCount(info.bits), |
| ... | ... | @@ -2865,7 +2957,7 @@ pub const Value = extern union { |
| 2865 | 2957 | if (lhs.isUndef() or rhs.isUndef()) return Value.initTag(.undef); |
| 2866 | 2958 | |
| 2867 | 2959 | if (ty.zigTypeTag() == .ComptimeInt) { |
| 2868 | return intAdd(lhs, rhs, ty, arena); | |
| 2960 | return intAdd(lhs, rhs, ty, arena, target); | |
| 2869 | 2961 | } |
| 2870 | 2962 | |
| 2871 | 2963 | if (ty.isAnyFloat()) { |
| ... | ... | @@ -2925,8 +3017,8 @@ pub const Value = extern union { |
| 2925 | 3017 | |
| 2926 | 3018 | var lhs_space: Value.BigIntSpace = undefined; |
| 2927 | 3019 | var rhs_space: Value.BigIntSpace = undefined; |
| 2928 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 2929 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3020 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3021 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 2930 | 3022 | const limbs = try arena.alloc( |
| 2931 | 3023 | std.math.big.Limb, |
| 2932 | 3024 | std.math.big.int.calcTwosCompLimbCount(info.bits), |
| ... | ... | @@ -2947,8 +3039,8 @@ pub const Value = extern union { |
| 2947 | 3039 | |
| 2948 | 3040 | var lhs_space: Value.BigIntSpace = undefined; |
| 2949 | 3041 | var rhs_space: Value.BigIntSpace = undefined; |
| 2950 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 2951 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3042 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3043 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 2952 | 3044 | const limbs = try arena.alloc( |
| 2953 | 3045 | std.math.big.Limb, |
| 2954 | 3046 | std.math.big.int.calcTwosCompLimbCount(info.bits), |
| ... | ... | @@ -2991,7 +3083,7 @@ pub const Value = extern union { |
| 2991 | 3083 | if (lhs.isUndef() or rhs.isUndef()) return Value.initTag(.undef); |
| 2992 | 3084 | |
| 2993 | 3085 | if (ty.zigTypeTag() == .ComptimeInt) { |
| 2994 | return intSub(lhs, rhs, ty, arena); | |
| 3086 | return intSub(lhs, rhs, ty, arena, target); | |
| 2995 | 3087 | } |
| 2996 | 3088 | |
| 2997 | 3089 | if (ty.isAnyFloat()) { |
| ... | ... | @@ -3035,8 +3127,8 @@ pub const Value = extern union { |
| 3035 | 3127 | |
| 3036 | 3128 | var lhs_space: Value.BigIntSpace = undefined; |
| 3037 | 3129 | var rhs_space: Value.BigIntSpace = undefined; |
| 3038 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3039 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3130 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3131 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3040 | 3132 | const limbs = try arena.alloc( |
| 3041 | 3133 | std.math.big.Limb, |
| 3042 | 3134 | std.math.big.int.calcTwosCompLimbCount(info.bits), |
| ... | ... | @@ -3057,8 +3149,8 @@ pub const Value = extern union { |
| 3057 | 3149 | |
| 3058 | 3150 | var lhs_space: Value.BigIntSpace = undefined; |
| 3059 | 3151 | var rhs_space: Value.BigIntSpace = undefined; |
| 3060 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3061 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3152 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3153 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3062 | 3154 | const limbs = try arena.alloc( |
| 3063 | 3155 | std.math.big.Limb, |
| 3064 | 3156 | lhs_bigint.limbs.len + rhs_bigint.limbs.len, |
| ... | ... | @@ -3110,7 +3202,7 @@ pub const Value = extern union { |
| 3110 | 3202 | if (lhs.isUndef() or rhs.isUndef()) return Value.initTag(.undef); |
| 3111 | 3203 | |
| 3112 | 3204 | if (ty.zigTypeTag() == .ComptimeInt) { |
| 3113 | return intMul(lhs, rhs, ty, arena); | |
| 3205 | return intMul(lhs, rhs, ty, arena, target); | |
| 3114 | 3206 | } |
| 3115 | 3207 | |
| 3116 | 3208 | if (ty.isAnyFloat()) { |
| ... | ... | @@ -3154,8 +3246,8 @@ pub const Value = extern union { |
| 3154 | 3246 | |
| 3155 | 3247 | var lhs_space: Value.BigIntSpace = undefined; |
| 3156 | 3248 | var rhs_space: Value.BigIntSpace = undefined; |
| 3157 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3158 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3249 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3250 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3159 | 3251 | const limbs = try arena.alloc( |
| 3160 | 3252 | std.math.big.Limb, |
| 3161 | 3253 | std.math.max( |
| ... | ... | @@ -3175,24 +3267,24 @@ pub const Value = extern union { |
| 3175 | 3267 | } |
| 3176 | 3268 | |
| 3177 | 3269 | /// Supports both floats and ints; handles undefined. |
| 3178 | pub fn numberMax(lhs: Value, rhs: Value) Value { | |
| 3270 | pub fn numberMax(lhs: Value, rhs: Value, target: Target) Value { | |
| 3179 | 3271 | if (lhs.isUndef() or rhs.isUndef()) return undef; |
| 3180 | 3272 | if (lhs.isNan()) return rhs; |
| 3181 | 3273 | if (rhs.isNan()) return lhs; |
| 3182 | 3274 | |
| 3183 | return switch (order(lhs, rhs)) { | |
| 3275 | return switch (order(lhs, rhs, target)) { | |
| 3184 | 3276 | .lt => rhs, |
| 3185 | 3277 | .gt, .eq => lhs, |
| 3186 | 3278 | }; |
| 3187 | 3279 | } |
| 3188 | 3280 | |
| 3189 | 3281 | /// Supports both floats and ints; handles undefined. |
| 3190 | pub fn numberMin(lhs: Value, rhs: Value) Value { | |
| 3282 | pub fn numberMin(lhs: Value, rhs: Value, target: Target) Value { | |
| 3191 | 3283 | if (lhs.isUndef() or rhs.isUndef()) return undef; |
| 3192 | 3284 | if (lhs.isNan()) return rhs; |
| 3193 | 3285 | if (rhs.isNan()) return lhs; |
| 3194 | 3286 | |
| 3195 | return switch (order(lhs, rhs)) { | |
| 3287 | return switch (order(lhs, rhs, target)) { | |
| 3196 | 3288 | .lt => lhs, |
| 3197 | 3289 | .gt, .eq => rhs, |
| 3198 | 3290 | }; |
| ... | ... | @@ -3224,7 +3316,7 @@ pub const Value = extern union { |
| 3224 | 3316 | // TODO is this a performance issue? maybe we should try the operation without |
| 3225 | 3317 | // resorting to BigInt first. |
| 3226 | 3318 | var val_space: Value.BigIntSpace = undefined; |
| 3227 | const val_bigint = val.toBigInt(&val_space); | |
| 3319 | const val_bigint = val.toBigInt(&val_space, target); | |
| 3228 | 3320 | const limbs = try arena.alloc( |
| 3229 | 3321 | std.math.big.Limb, |
| 3230 | 3322 | std.math.big.int.calcTwosCompLimbCount(info.bits), |
| ... | ... | @@ -3236,27 +3328,27 @@ pub const Value = extern union { |
| 3236 | 3328 | } |
| 3237 | 3329 | |
| 3238 | 3330 | /// operands must be (vectors of) integers; handles undefined scalars. |
| 3239 | pub fn bitwiseAnd(lhs: Value, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 3331 | pub fn bitwiseAnd(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 3240 | 3332 | if (ty.zigTypeTag() == .Vector) { |
| 3241 | 3333 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3242 | 3334 | for (result_data) |*scalar, i| { |
| 3243 | scalar.* = try bitwiseAndScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator); | |
| 3335 | scalar.* = try bitwiseAndScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator, target); | |
| 3244 | 3336 | } |
| 3245 | 3337 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3246 | 3338 | } |
| 3247 | return bitwiseAndScalar(lhs, rhs, allocator); | |
| 3339 | return bitwiseAndScalar(lhs, rhs, allocator, target); | |
| 3248 | 3340 | } |
| 3249 | 3341 | |
| 3250 | 3342 | /// operands must be integers; handles undefined. |
| 3251 | pub fn bitwiseAndScalar(lhs: Value, rhs: Value, arena: Allocator) !Value { | |
| 3343 | pub fn bitwiseAndScalar(lhs: Value, rhs: Value, arena: Allocator, target: Target) !Value { | |
| 3252 | 3344 | if (lhs.isUndef() or rhs.isUndef()) return Value.initTag(.undef); |
| 3253 | 3345 | |
| 3254 | 3346 | // TODO is this a performance issue? maybe we should try the operation without |
| 3255 | 3347 | // resorting to BigInt first. |
| 3256 | 3348 | var lhs_space: Value.BigIntSpace = undefined; |
| 3257 | 3349 | var rhs_space: Value.BigIntSpace = undefined; |
| 3258 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3259 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3350 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3351 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3260 | 3352 | const limbs = try arena.alloc( |
| 3261 | 3353 | std.math.big.Limb, |
| 3262 | 3354 | // + 1 for negatives |
| ... | ... | @@ -3283,38 +3375,38 @@ pub const Value = extern union { |
| 3283 | 3375 | pub fn bitwiseNandScalar(lhs: Value, rhs: Value, ty: Type, arena: Allocator, target: Target) !Value { |
| 3284 | 3376 | if (lhs.isUndef() or rhs.isUndef()) return Value.initTag(.undef); |
| 3285 | 3377 | |
| 3286 | const anded = try bitwiseAnd(lhs, rhs, ty, arena); | |
| 3378 | const anded = try bitwiseAnd(lhs, rhs, ty, arena, target); | |
| 3287 | 3379 | |
| 3288 | 3380 | const all_ones = if (ty.isSignedInt()) |
| 3289 | 3381 | try Value.Tag.int_i64.create(arena, -1) |
| 3290 | 3382 | else |
| 3291 | 3383 | try ty.maxInt(arena, target); |
| 3292 | 3384 | |
| 3293 | return bitwiseXor(anded, all_ones, ty, arena); | |
| 3385 | return bitwiseXor(anded, all_ones, ty, arena, target); | |
| 3294 | 3386 | } |
| 3295 | 3387 | |
| 3296 | 3388 | /// operands must be (vectors of) integers; handles undefined scalars. |
| 3297 | pub fn bitwiseOr(lhs: Value, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 3389 | pub fn bitwiseOr(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 3298 | 3390 | if (ty.zigTypeTag() == .Vector) { |
| 3299 | 3391 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3300 | 3392 | for (result_data) |*scalar, i| { |
| 3301 | scalar.* = try bitwiseOrScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator); | |
| 3393 | scalar.* = try bitwiseOrScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator, target); | |
| 3302 | 3394 | } |
| 3303 | 3395 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3304 | 3396 | } |
| 3305 | return bitwiseOrScalar(lhs, rhs, allocator); | |
| 3397 | return bitwiseOrScalar(lhs, rhs, allocator, target); | |
| 3306 | 3398 | } |
| 3307 | 3399 | |
| 3308 | 3400 | /// operands must be integers; handles undefined. |
| 3309 | pub fn bitwiseOrScalar(lhs: Value, rhs: Value, arena: Allocator) !Value { | |
| 3401 | pub fn bitwiseOrScalar(lhs: Value, rhs: Value, arena: Allocator, target: Target) !Value { | |
| 3310 | 3402 | if (lhs.isUndef() or rhs.isUndef()) return Value.initTag(.undef); |
| 3311 | 3403 | |
| 3312 | 3404 | // TODO is this a performance issue? maybe we should try the operation without |
| 3313 | 3405 | // resorting to BigInt first. |
| 3314 | 3406 | var lhs_space: Value.BigIntSpace = undefined; |
| 3315 | 3407 | var rhs_space: Value.BigIntSpace = undefined; |
| 3316 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3317 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3408 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3409 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3318 | 3410 | const limbs = try arena.alloc( |
| 3319 | 3411 | std.math.big.Limb, |
| 3320 | 3412 | std.math.max(lhs_bigint.limbs.len, rhs_bigint.limbs.len), |
| ... | ... | @@ -3325,27 +3417,27 @@ pub const Value = extern union { |
| 3325 | 3417 | } |
| 3326 | 3418 | |
| 3327 | 3419 | /// operands must be (vectors of) integers; handles undefined scalars. |
| 3328 | pub fn bitwiseXor(lhs: Value, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 3420 | pub fn bitwiseXor(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 3329 | 3421 | if (ty.zigTypeTag() == .Vector) { |
| 3330 | 3422 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3331 | 3423 | for (result_data) |*scalar, i| { |
| 3332 | scalar.* = try bitwiseXorScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator); | |
| 3424 | scalar.* = try bitwiseXorScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator, target); | |
| 3333 | 3425 | } |
| 3334 | 3426 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3335 | 3427 | } |
| 3336 | return bitwiseXorScalar(lhs, rhs, allocator); | |
| 3428 | return bitwiseXorScalar(lhs, rhs, allocator, target); | |
| 3337 | 3429 | } |
| 3338 | 3430 | |
| 3339 | 3431 | /// operands must be integers; handles undefined. |
| 3340 | pub fn bitwiseXorScalar(lhs: Value, rhs: Value, arena: Allocator) !Value { | |
| 3432 | pub fn bitwiseXorScalar(lhs: Value, rhs: Value, arena: Allocator, target: Target) !Value { | |
| 3341 | 3433 | if (lhs.isUndef() or rhs.isUndef()) return Value.initTag(.undef); |
| 3342 | 3434 | |
| 3343 | 3435 | // TODO is this a performance issue? maybe we should try the operation without |
| 3344 | 3436 | // resorting to BigInt first. |
| 3345 | 3437 | var lhs_space: Value.BigIntSpace = undefined; |
| 3346 | 3438 | var rhs_space: Value.BigIntSpace = undefined; |
| 3347 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3348 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3439 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3440 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3349 | 3441 | const limbs = try arena.alloc( |
| 3350 | 3442 | std.math.big.Limb, |
| 3351 | 3443 | // + 1 for negatives |
| ... | ... | @@ -3356,24 +3448,24 @@ pub const Value = extern union { |
| 3356 | 3448 | return fromBigInt(arena, result_bigint.toConst()); |
| 3357 | 3449 | } |
| 3358 | 3450 | |
| 3359 | pub fn intAdd(lhs: Value, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 3451 | pub fn intAdd(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 3360 | 3452 | if (ty.zigTypeTag() == .Vector) { |
| 3361 | 3453 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3362 | 3454 | for (result_data) |*scalar, i| { |
| 3363 | scalar.* = try intAddScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator); | |
| 3455 | scalar.* = try intAddScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator, target); | |
| 3364 | 3456 | } |
| 3365 | 3457 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3366 | 3458 | } |
| 3367 | return intAddScalar(lhs, rhs, allocator); | |
| 3459 | return intAddScalar(lhs, rhs, allocator, target); | |
| 3368 | 3460 | } |
| 3369 | 3461 | |
| 3370 | pub fn intAddScalar(lhs: Value, rhs: Value, allocator: Allocator) !Value { | |
| 3462 | pub fn intAddScalar(lhs: Value, rhs: Value, allocator: Allocator, target: Target) !Value { | |
| 3371 | 3463 | // TODO is this a performance issue? maybe we should try the operation without |
| 3372 | 3464 | // resorting to BigInt first. |
| 3373 | 3465 | var lhs_space: Value.BigIntSpace = undefined; |
| 3374 | 3466 | var rhs_space: Value.BigIntSpace = undefined; |
| 3375 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3376 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3467 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3468 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3377 | 3469 | const limbs = try allocator.alloc( |
| 3378 | 3470 | std.math.big.Limb, |
| 3379 | 3471 | std.math.max(lhs_bigint.limbs.len, rhs_bigint.limbs.len) + 1, |
| ... | ... | @@ -3383,24 +3475,24 @@ pub const Value = extern union { |
| 3383 | 3475 | return fromBigInt(allocator, result_bigint.toConst()); |
| 3384 | 3476 | } |
| 3385 | 3477 | |
| 3386 | pub fn intSub(lhs: Value, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 3478 | pub fn intSub(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 3387 | 3479 | if (ty.zigTypeTag() == .Vector) { |
| 3388 | 3480 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3389 | 3481 | for (result_data) |*scalar, i| { |
| 3390 | scalar.* = try intSubScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator); | |
| 3482 | scalar.* = try intSubScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator, target); | |
| 3391 | 3483 | } |
| 3392 | 3484 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3393 | 3485 | } |
| 3394 | return intSubScalar(lhs, rhs, allocator); | |
| 3486 | return intSubScalar(lhs, rhs, allocator, target); | |
| 3395 | 3487 | } |
| 3396 | 3488 | |
| 3397 | pub fn intSubScalar(lhs: Value, rhs: Value, allocator: Allocator) !Value { | |
| 3489 | pub fn intSubScalar(lhs: Value, rhs: Value, allocator: Allocator, target: Target) !Value { | |
| 3398 | 3490 | // TODO is this a performance issue? maybe we should try the operation without |
| 3399 | 3491 | // resorting to BigInt first. |
| 3400 | 3492 | var lhs_space: Value.BigIntSpace = undefined; |
| 3401 | 3493 | var rhs_space: Value.BigIntSpace = undefined; |
| 3402 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3403 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3494 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3495 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3404 | 3496 | const limbs = try allocator.alloc( |
| 3405 | 3497 | std.math.big.Limb, |
| 3406 | 3498 | std.math.max(lhs_bigint.limbs.len, rhs_bigint.limbs.len) + 1, |
| ... | ... | @@ -3410,24 +3502,24 @@ pub const Value = extern union { |
| 3410 | 3502 | return fromBigInt(allocator, result_bigint.toConst()); |
| 3411 | 3503 | } |
| 3412 | 3504 | |
| 3413 | pub fn intDiv(lhs: Value, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 3505 | pub fn intDiv(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 3414 | 3506 | if (ty.zigTypeTag() == .Vector) { |
| 3415 | 3507 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3416 | 3508 | for (result_data) |*scalar, i| { |
| 3417 | scalar.* = try intDivScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator); | |
| 3509 | scalar.* = try intDivScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator, target); | |
| 3418 | 3510 | } |
| 3419 | 3511 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3420 | 3512 | } |
| 3421 | return intDivScalar(lhs, rhs, allocator); | |
| 3513 | return intDivScalar(lhs, rhs, allocator, target); | |
| 3422 | 3514 | } |
| 3423 | 3515 | |
| 3424 | pub fn intDivScalar(lhs: Value, rhs: Value, allocator: Allocator) !Value { | |
| 3516 | pub fn intDivScalar(lhs: Value, rhs: Value, allocator: Allocator, target: Target) !Value { | |
| 3425 | 3517 | // TODO is this a performance issue? maybe we should try the operation without |
| 3426 | 3518 | // resorting to BigInt first. |
| 3427 | 3519 | var lhs_space: Value.BigIntSpace = undefined; |
| 3428 | 3520 | var rhs_space: Value.BigIntSpace = undefined; |
| 3429 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3430 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3521 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3522 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3431 | 3523 | const limbs_q = try allocator.alloc( |
| 3432 | 3524 | std.math.big.Limb, |
| 3433 | 3525 | lhs_bigint.limbs.len, |
| ... | ... | @@ -3446,24 +3538,24 @@ pub const Value = extern union { |
| 3446 | 3538 | return fromBigInt(allocator, result_q.toConst()); |
| 3447 | 3539 | } |
| 3448 | 3540 | |
| 3449 | pub fn intDivFloor(lhs: Value, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 3541 | pub fn intDivFloor(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 3450 | 3542 | if (ty.zigTypeTag() == .Vector) { |
| 3451 | 3543 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3452 | 3544 | for (result_data) |*scalar, i| { |
| 3453 | scalar.* = try intDivFloorScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator); | |
| 3545 | scalar.* = try intDivFloorScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator, target); | |
| 3454 | 3546 | } |
| 3455 | 3547 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3456 | 3548 | } |
| 3457 | return intDivFloorScalar(lhs, rhs, allocator); | |
| 3549 | return intDivFloorScalar(lhs, rhs, allocator, target); | |
| 3458 | 3550 | } |
| 3459 | 3551 | |
| 3460 | pub fn intDivFloorScalar(lhs: Value, rhs: Value, allocator: Allocator) !Value { | |
| 3552 | pub fn intDivFloorScalar(lhs: Value, rhs: Value, allocator: Allocator, target: Target) !Value { | |
| 3461 | 3553 | // TODO is this a performance issue? maybe we should try the operation without |
| 3462 | 3554 | // resorting to BigInt first. |
| 3463 | 3555 | var lhs_space: Value.BigIntSpace = undefined; |
| 3464 | 3556 | var rhs_space: Value.BigIntSpace = undefined; |
| 3465 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3466 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3557 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3558 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3467 | 3559 | const limbs_q = try allocator.alloc( |
| 3468 | 3560 | std.math.big.Limb, |
| 3469 | 3561 | lhs_bigint.limbs.len, |
| ... | ... | @@ -3482,24 +3574,24 @@ pub const Value = extern union { |
| 3482 | 3574 | return fromBigInt(allocator, result_q.toConst()); |
| 3483 | 3575 | } |
| 3484 | 3576 | |
| 3485 | pub fn intRem(lhs: Value, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 3577 | pub fn intRem(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 3486 | 3578 | if (ty.zigTypeTag() == .Vector) { |
| 3487 | 3579 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3488 | 3580 | for (result_data) |*scalar, i| { |
| 3489 | scalar.* = try intRemScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator); | |
| 3581 | scalar.* = try intRemScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator, target); | |
| 3490 | 3582 | } |
| 3491 | 3583 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3492 | 3584 | } |
| 3493 | return intRemScalar(lhs, rhs, allocator); | |
| 3585 | return intRemScalar(lhs, rhs, allocator, target); | |
| 3494 | 3586 | } |
| 3495 | 3587 | |
| 3496 | pub fn intRemScalar(lhs: Value, rhs: Value, allocator: Allocator) !Value { | |
| 3588 | pub fn intRemScalar(lhs: Value, rhs: Value, allocator: Allocator, target: Target) !Value { | |
| 3497 | 3589 | // TODO is this a performance issue? maybe we should try the operation without |
| 3498 | 3590 | // resorting to BigInt first. |
| 3499 | 3591 | var lhs_space: Value.BigIntSpace = undefined; |
| 3500 | 3592 | var rhs_space: Value.BigIntSpace = undefined; |
| 3501 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3502 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3593 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3594 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3503 | 3595 | const limbs_q = try allocator.alloc( |
| 3504 | 3596 | std.math.big.Limb, |
| 3505 | 3597 | lhs_bigint.limbs.len, |
| ... | ... | @@ -3520,24 +3612,24 @@ pub const Value = extern union { |
| 3520 | 3612 | return fromBigInt(allocator, result_r.toConst()); |
| 3521 | 3613 | } |
| 3522 | 3614 | |
| 3523 | pub fn intMod(lhs: Value, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 3615 | pub fn intMod(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 3524 | 3616 | if (ty.zigTypeTag() == .Vector) { |
| 3525 | 3617 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3526 | 3618 | for (result_data) |*scalar, i| { |
| 3527 | scalar.* = try intModScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator); | |
| 3619 | scalar.* = try intModScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator, target); | |
| 3528 | 3620 | } |
| 3529 | 3621 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3530 | 3622 | } |
| 3531 | return intModScalar(lhs, rhs, allocator); | |
| 3623 | return intModScalar(lhs, rhs, allocator, target); | |
| 3532 | 3624 | } |
| 3533 | 3625 | |
| 3534 | pub fn intModScalar(lhs: Value, rhs: Value, allocator: Allocator) !Value { | |
| 3626 | pub fn intModScalar(lhs: Value, rhs: Value, allocator: Allocator, target: Target) !Value { | |
| 3535 | 3627 | // TODO is this a performance issue? maybe we should try the operation without |
| 3536 | 3628 | // resorting to BigInt first. |
| 3537 | 3629 | var lhs_space: Value.BigIntSpace = undefined; |
| 3538 | 3630 | var rhs_space: Value.BigIntSpace = undefined; |
| 3539 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3540 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3631 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3632 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3541 | 3633 | const limbs_q = try allocator.alloc( |
| 3542 | 3634 | std.math.big.Limb, |
| 3543 | 3635 | lhs_bigint.limbs.len, |
| ... | ... | @@ -3658,24 +3750,24 @@ pub const Value = extern union { |
| 3658 | 3750 | } |
| 3659 | 3751 | } |
| 3660 | 3752 | |
| 3661 | pub fn intMul(lhs: Value, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 3753 | pub fn intMul(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 3662 | 3754 | if (ty.zigTypeTag() == .Vector) { |
| 3663 | 3755 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3664 | 3756 | for (result_data) |*scalar, i| { |
| 3665 | scalar.* = try intMulScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator); | |
| 3757 | scalar.* = try intMulScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator, target); | |
| 3666 | 3758 | } |
| 3667 | 3759 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3668 | 3760 | } |
| 3669 | return intMulScalar(lhs, rhs, allocator); | |
| 3761 | return intMulScalar(lhs, rhs, allocator, target); | |
| 3670 | 3762 | } |
| 3671 | 3763 | |
| 3672 | pub fn intMulScalar(lhs: Value, rhs: Value, allocator: Allocator) !Value { | |
| 3764 | pub fn intMulScalar(lhs: Value, rhs: Value, allocator: Allocator, target: Target) !Value { | |
| 3673 | 3765 | // TODO is this a performance issue? maybe we should try the operation without |
| 3674 | 3766 | // resorting to BigInt first. |
| 3675 | 3767 | var lhs_space: Value.BigIntSpace = undefined; |
| 3676 | 3768 | var rhs_space: Value.BigIntSpace = undefined; |
| 3677 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3678 | const rhs_bigint = rhs.toBigInt(&rhs_space); | |
| 3769 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3770 | const rhs_bigint = rhs.toBigInt(&rhs_space, target); | |
| 3679 | 3771 | const limbs = try allocator.alloc( |
| 3680 | 3772 | std.math.big.Limb, |
| 3681 | 3773 | lhs_bigint.limbs.len + rhs_bigint.limbs.len, |
| ... | ... | @@ -3690,34 +3782,41 @@ pub const Value = extern union { |
| 3690 | 3782 | return fromBigInt(allocator, result_bigint.toConst()); |
| 3691 | 3783 | } |
| 3692 | 3784 | |
| 3693 | pub fn intTrunc(val: Value, ty: Type, allocator: Allocator, signedness: std.builtin.Signedness, bits: u16) !Value { | |
| 3785 | pub fn intTrunc(val: Value, ty: Type, allocator: Allocator, signedness: std.builtin.Signedness, bits: u16, target: Target) !Value { | |
| 3694 | 3786 | if (ty.zigTypeTag() == .Vector) { |
| 3695 | 3787 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3696 | 3788 | for (result_data) |*scalar, i| { |
| 3697 | scalar.* = try intTruncScalar(val.indexVectorlike(i), allocator, signedness, bits); | |
| 3789 | scalar.* = try intTruncScalar(val.indexVectorlike(i), allocator, signedness, bits, target); | |
| 3698 | 3790 | } |
| 3699 | 3791 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3700 | 3792 | } |
| 3701 | return intTruncScalar(val, allocator, signedness, bits); | |
| 3793 | return intTruncScalar(val, allocator, signedness, bits, target); | |
| 3702 | 3794 | } |
| 3703 | 3795 | |
| 3704 | 3796 | /// This variant may vectorize on `bits`. Asserts that `bits` is a (vector of) `u16`. |
| 3705 | pub fn intTruncBitsAsValue(val: Value, ty: Type, allocator: Allocator, signedness: std.builtin.Signedness, bits: Value) !Value { | |
| 3797 | pub fn intTruncBitsAsValue( | |
| 3798 | val: Value, | |
| 3799 | ty: Type, | |
| 3800 | allocator: Allocator, | |
| 3801 | signedness: std.builtin.Signedness, | |
| 3802 | bits: Value, | |
| 3803 | target: Target, | |
| 3804 | ) !Value { | |
| 3706 | 3805 | if (ty.zigTypeTag() == .Vector) { |
| 3707 | 3806 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3708 | 3807 | for (result_data) |*scalar, i| { |
| 3709 | scalar.* = try intTruncScalar(val.indexVectorlike(i), allocator, signedness, @intCast(u16, bits.indexVectorlike(i).toUnsignedInt())); | |
| 3808 | scalar.* = try intTruncScalar(val.indexVectorlike(i), allocator, signedness, @intCast(u16, bits.indexVectorlike(i).toUnsignedInt(target)), target); | |
| 3710 | 3809 | } |
| 3711 | 3810 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3712 | 3811 | } |
| 3713 | return intTruncScalar(val, allocator, signedness, @intCast(u16, bits.toUnsignedInt())); | |
| 3812 | return intTruncScalar(val, allocator, signedness, @intCast(u16, bits.toUnsignedInt(target)), target); | |
| 3714 | 3813 | } |
| 3715 | 3814 | |
| 3716 | pub fn intTruncScalar(val: Value, allocator: Allocator, signedness: std.builtin.Signedness, bits: u16) !Value { | |
| 3815 | pub fn intTruncScalar(val: Value, allocator: Allocator, signedness: std.builtin.Signedness, bits: u16, target: Target) !Value { | |
| 3717 | 3816 | if (bits == 0) return Value.zero; |
| 3718 | 3817 | |
| 3719 | 3818 | var val_space: Value.BigIntSpace = undefined; |
| 3720 | const val_bigint = val.toBigInt(&val_space); | |
| 3819 | const val_bigint = val.toBigInt(&val_space, target); | |
| 3721 | 3820 | |
| 3722 | 3821 | const limbs = try allocator.alloc( |
| 3723 | 3822 | std.math.big.Limb, |
| ... | ... | @@ -3729,23 +3828,23 @@ pub const Value = extern union { |
| 3729 | 3828 | return fromBigInt(allocator, result_bigint.toConst()); |
| 3730 | 3829 | } |
| 3731 | 3830 | |
| 3732 | pub fn shl(lhs: Value, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 3831 | pub fn shl(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 3733 | 3832 | if (ty.zigTypeTag() == .Vector) { |
| 3734 | 3833 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3735 | 3834 | for (result_data) |*scalar, i| { |
| 3736 | scalar.* = try shlScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator); | |
| 3835 | scalar.* = try shlScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator, target); | |
| 3737 | 3836 | } |
| 3738 | 3837 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3739 | 3838 | } |
| 3740 | return shlScalar(lhs, rhs, allocator); | |
| 3839 | return shlScalar(lhs, rhs, allocator, target); | |
| 3741 | 3840 | } |
| 3742 | 3841 | |
| 3743 | pub fn shlScalar(lhs: Value, rhs: Value, allocator: Allocator) !Value { | |
| 3842 | pub fn shlScalar(lhs: Value, rhs: Value, allocator: Allocator, target: Target) !Value { | |
| 3744 | 3843 | // TODO is this a performance issue? maybe we should try the operation without |
| 3745 | 3844 | // resorting to BigInt first. |
| 3746 | 3845 | var lhs_space: Value.BigIntSpace = undefined; |
| 3747 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3748 | const shift = @intCast(usize, rhs.toUnsignedInt()); | |
| 3846 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3847 | const shift = @intCast(usize, rhs.toUnsignedInt(target)); | |
| 3749 | 3848 | const limbs = try allocator.alloc( |
| 3750 | 3849 | std.math.big.Limb, |
| 3751 | 3850 | lhs_bigint.limbs.len + (shift / (@sizeOf(std.math.big.Limb) * 8)) + 1, |
| ... | ... | @@ -3768,8 +3867,8 @@ pub const Value = extern union { |
| 3768 | 3867 | ) !OverflowArithmeticResult { |
| 3769 | 3868 | const info = ty.intInfo(target); |
| 3770 | 3869 | var lhs_space: Value.BigIntSpace = undefined; |
| 3771 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3772 | const shift = @intCast(usize, rhs.toUnsignedInt()); | |
| 3870 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3871 | const shift = @intCast(usize, rhs.toUnsignedInt(target)); | |
| 3773 | 3872 | const limbs = try allocator.alloc( |
| 3774 | 3873 | std.math.big.Limb, |
| 3775 | 3874 | lhs_bigint.limbs.len + (shift / (@sizeOf(std.math.big.Limb) * 8)) + 1, |
| ... | ... | @@ -3819,8 +3918,8 @@ pub const Value = extern union { |
| 3819 | 3918 | const info = ty.intInfo(target); |
| 3820 | 3919 | |
| 3821 | 3920 | var lhs_space: Value.BigIntSpace = undefined; |
| 3822 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3823 | const shift = @intCast(usize, rhs.toUnsignedInt()); | |
| 3921 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3922 | const shift = @intCast(usize, rhs.toUnsignedInt(target)); | |
| 3824 | 3923 | const limbs = try arena.alloc( |
| 3825 | 3924 | std.math.big.Limb, |
| 3826 | 3925 | std.math.big.int.calcTwosCompLimbCount(info.bits), |
| ... | ... | @@ -3858,29 +3957,29 @@ pub const Value = extern union { |
| 3858 | 3957 | arena: Allocator, |
| 3859 | 3958 | target: Target, |
| 3860 | 3959 | ) !Value { |
| 3861 | const shifted = try lhs.shl(rhs, ty, arena); | |
| 3960 | const shifted = try lhs.shl(rhs, ty, arena, target); | |
| 3862 | 3961 | const int_info = ty.intInfo(target); |
| 3863 | const truncated = try shifted.intTrunc(ty, arena, int_info.signedness, int_info.bits); | |
| 3962 | const truncated = try shifted.intTrunc(ty, arena, int_info.signedness, int_info.bits, target); | |
| 3864 | 3963 | return truncated; |
| 3865 | 3964 | } |
| 3866 | 3965 | |
| 3867 | pub fn shr(lhs: Value, rhs: Value, ty: Type, allocator: Allocator) !Value { | |
| 3966 | pub fn shr(lhs: Value, rhs: Value, ty: Type, allocator: Allocator, target: Target) !Value { | |
| 3868 | 3967 | if (ty.zigTypeTag() == .Vector) { |
| 3869 | 3968 | const result_data = try allocator.alloc(Value, ty.vectorLen()); |
| 3870 | 3969 | for (result_data) |*scalar, i| { |
| 3871 | scalar.* = try shrScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator); | |
| 3970 | scalar.* = try shrScalar(lhs.indexVectorlike(i), rhs.indexVectorlike(i), allocator, target); | |
| 3872 | 3971 | } |
| 3873 | 3972 | return Value.Tag.aggregate.create(allocator, result_data); |
| 3874 | 3973 | } |
| 3875 | return shrScalar(lhs, rhs, allocator); | |
| 3974 | return shrScalar(lhs, rhs, allocator, target); | |
| 3876 | 3975 | } |
| 3877 | 3976 | |
| 3878 | pub fn shrScalar(lhs: Value, rhs: Value, allocator: Allocator) !Value { | |
| 3977 | pub fn shrScalar(lhs: Value, rhs: Value, allocator: Allocator, target: Target) !Value { | |
| 3879 | 3978 | // TODO is this a performance issue? maybe we should try the operation without |
| 3880 | 3979 | // resorting to BigInt first. |
| 3881 | 3980 | var lhs_space: Value.BigIntSpace = undefined; |
| 3882 | const lhs_bigint = lhs.toBigInt(&lhs_space); | |
| 3883 | const shift = @intCast(usize, rhs.toUnsignedInt()); | |
| 3981 | const lhs_bigint = lhs.toBigInt(&lhs_space, target); | |
| 3982 | const shift = @intCast(usize, rhs.toUnsignedInt(target)); | |
| 3884 | 3983 | |
| 3885 | 3984 | const result_limbs = lhs_bigint.limbs.len -| (shift / (@sizeOf(std.math.big.Limb) * 8)); |
| 3886 | 3985 | if (result_limbs == 0) { |
test/behavior.zig+1-1| ... | ... | @@ -125,6 +125,7 @@ test { |
| 125 | 125 | _ = @import("behavior/src.zig"); |
| 126 | 126 | _ = @import("behavior/struct.zig"); |
| 127 | 127 | _ = @import("behavior/struct_contains_null_ptr_itself.zig"); |
| 128 | _ = @import("behavior/struct_contains_slice_of_itself.zig"); | |
| 128 | 129 | _ = @import("behavior/switch.zig"); |
| 129 | 130 | _ = @import("behavior/switch_prong_err_enum.zig"); |
| 130 | 131 | _ = @import("behavior/switch_prong_implicit_cast.zig"); |
| ... | ... | @@ -179,6 +180,5 @@ test { |
| 179 | 180 | _ = @import("behavior/bugs/6781.zig"); |
| 180 | 181 | _ = @import("behavior/bugs/7027.zig"); |
| 181 | 182 | _ = @import("behavior/select.zig"); |
| 182 | _ = @import("behavior/struct_contains_slice_of_itself.zig"); | |
| 183 | 183 | } |
| 184 | 184 | } |
test/behavior/struct_contains_slice_of_itself.zig+9| ... | ... | @@ -1,3 +1,4 @@ |
| 1 | const builtin = @import("builtin"); | |
| 1 | 2 | const expect = @import("std").testing.expect; |
| 2 | 3 | |
| 3 | 4 | const Node = struct { |
| ... | ... | @@ -11,6 +12,10 @@ const NodeAligned = struct { |
| 11 | 12 | }; |
| 12 | 13 | |
| 13 | 14 | test "struct contains slice of itself" { |
| 15 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO | |
| 16 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | |
| 17 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | |
| 18 | ||
| 14 | 19 | var other_nodes = [_]Node{ |
| 15 | 20 | Node{ |
| 16 | 21 | .payload = 31, |
| ... | ... | @@ -48,6 +53,10 @@ test "struct contains slice of itself" { |
| 48 | 53 | } |
| 49 | 54 | |
| 50 | 55 | test "struct contains aligned slice of itself" { |
| 56 | if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO | |
| 57 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO | |
| 58 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO | |
| 59 | ||
| 51 | 60 | var other_nodes = [_]NodeAligned{ |
| 52 | 61 | NodeAligned{ |
| 53 | 62 | .payload = 31, |
test/stage2/x86_64.zig+1-1| ... | ... | @@ -1166,7 +1166,7 @@ pub fn addCases(ctx: *TestContext) !void { |
| 1166 | 1166 | \\ _ = x; |
| 1167 | 1167 | \\} |
| 1168 | 1168 | , &[_][]const u8{ |
| 1169 | ":2:9: error: variable of type '@Type(.Null)' must be const or comptime", | |
| 1169 | ":2:9: error: variable of type '@TypeOf(null)' must be const or comptime", | |
| 1170 | 1170 | }); |
| 1171 | 1171 | } |
| 1172 | 1172 |