| ... | ... | @@ -42,19 +42,15 @@ const TypeDescriptor = extern struct { |
| 42 | 42 | } |
| 43 | 43 | }; |
| 44 | 44 | |
| 45 | | const ValueHandle = *const opaque { |
| 46 | | fn getValue(handle: ValueHandle, data: anytype) Value { |
| 47 | | return .{ .handle = handle, .type_descriptor = data.type_descriptor }; |
| 48 | | } |
| 49 | | }; |
| 45 | const ValueHandle = *const opaque {}; |
| 50 | 46 | |
| 51 | 47 | const Value = extern struct { |
| 52 | | type_descriptor: *const TypeDescriptor, |
| 48 | td: *const TypeDescriptor, |
| 53 | 49 | handle: ValueHandle, |
| 54 | 50 | |
| 55 | 51 | fn getUnsignedInteger(value: Value) u128 { |
| 56 | | assert(!value.type_descriptor.isSigned()); |
| 57 | | const size = value.type_descriptor.getIntegerSize(); |
| 52 | assert(!value.td.isSigned()); |
| 53 | const size = value.td.getIntegerSize(); |
| 58 | 54 | const max_inline_size = @bitSizeOf(ValueHandle); |
| 59 | 55 | if (size <= max_inline_size) { |
| 60 | 56 | return @intFromPtr(value.handle); |
| ... | ... | @@ -68,8 +64,8 @@ const Value = extern struct { |
| 68 | 64 | } |
| 69 | 65 | |
| 70 | 66 | fn getSignedInteger(value: Value) i128 { |
| 71 | | assert(value.type_descriptor.isSigned()); |
| 72 | | const size = value.type_descriptor.getIntegerSize(); |
| 67 | assert(value.td.isSigned()); |
| 68 | const size = value.td.getIntegerSize(); |
| 73 | 69 | const max_inline_size = @bitSizeOf(ValueHandle); |
| 74 | 70 | if (size <= max_inline_size) { |
| 75 | 71 | const extra_bits: std.math.Log2Int(usize) = @intCast(max_inline_size - size); |
| ... | ... | @@ -84,8 +80,8 @@ const Value = extern struct { |
| 84 | 80 | } |
| 85 | 81 | |
| 86 | 82 | fn getFloat(value: Value) c_longdouble { |
| 87 | | assert(value.type_descriptor.kind == .float); |
| 88 | | const size = value.type_descriptor.info.float; |
| 83 | assert(value.td.kind == .float); |
| 84 | const size = value.td.info.float; |
| 89 | 85 | const max_inline_size = @bitSizeOf(ValueHandle); |
| 90 | 86 | if (size <= max_inline_size) { |
| 91 | 87 | return @bitCast(@intFromPtr(value.handle)); |
| ... | ... | @@ -99,17 +95,17 @@ const Value = extern struct { |
| 99 | 95 | } |
| 100 | 96 | |
| 101 | 97 | fn isMinusOne(value: Value) bool { |
| 102 | | return value.type_descriptor.isSigned() and |
| 98 | return value.td.isSigned() and |
| 103 | 99 | value.getSignedInteger() == -1; |
| 104 | 100 | } |
| 105 | 101 | |
| 106 | 102 | fn isNegative(value: Value) bool { |
| 107 | | return value.type_descriptor.isSigned() and |
| 103 | return value.td.isSigned() and |
| 108 | 104 | value.getSignedInteger() < 0; |
| 109 | 105 | } |
| 110 | 106 | |
| 111 | 107 | fn getPositiveInteger(value: Value) u128 { |
| 112 | | if (value.type_descriptor.isSigned()) { |
| 108 | if (value.td.isSigned()) { |
| 113 | 109 | const signed = value.getSignedInteger(); |
| 114 | 110 | assert(signed >= 0); |
| 115 | 111 | return @intCast(signed); |
| ... | ... | @@ -126,9 +122,9 @@ const Value = extern struct { |
| 126 | 122 | ) !void { |
| 127 | 123 | comptime assert(fmt.len == 0); |
| 128 | 124 | |
| 129 | | switch (value.type_descriptor.kind) { |
| 125 | switch (value.td.kind) { |
| 130 | 126 | .integer => { |
| 131 | | if (value.type_descriptor.isSigned()) { |
| 127 | if (value.td.isSigned()) { |
| 132 | 128 | try writer.print("{}", .{value.getSignedInteger()}); |
| 133 | 129 | } else { |
| 134 | 130 | try writer.print("{}", .{value.getUnsignedInteger()}); |
| ... | ... | @@ -142,7 +138,7 @@ const Value = extern struct { |
| 142 | 138 | |
| 143 | 139 | const OverflowData = extern struct { |
| 144 | 140 | loc: SourceLocation, |
| 145 | | type_descriptor: *const TypeDescriptor, |
| 141 | td: *const TypeDescriptor, |
| 146 | 142 | }; |
| 147 | 143 | |
| 148 | 144 | fn overflowHandler( |
| ... | ... | @@ -155,10 +151,10 @@ fn overflowHandler( |
| 155 | 151 | lhs_handle: ValueHandle, |
| 156 | 152 | rhs_handle: ValueHandle, |
| 157 | 153 | ) callconv(.c) noreturn { |
| 158 | | const lhs = lhs_handle.getValue(data); |
| 159 | | const rhs = rhs_handle.getValue(data); |
| 154 | const lhs: Value = .{ .handle = lhs_handle, .td = data.td }; |
| 155 | const rhs: Value = .{ .handle = rhs_handle, .td = data.td }; |
| 160 | 156 | |
| 161 | | const is_signed = data.type_descriptor.isSigned(); |
| 157 | const is_signed = data.td.isSigned(); |
| 162 | 158 | const fmt = "{s} integer overflow: " ++ "{} " ++ |
| 163 | 159 | operator ++ " {} cannot be represented in type {s}"; |
| 164 | 160 | |
| ... | ... | @@ -166,7 +162,7 @@ fn overflowHandler( |
| 166 | 162 | if (is_signed) "signed" else "unsigned", |
| 167 | 163 | lhs, |
| 168 | 164 | rhs, |
| 169 | | data.type_descriptor.getName(), |
| 165 | data.td.getName(), |
| 170 | 166 | }); |
| 171 | 167 | } |
| 172 | 168 | }; |
| ... | ... | @@ -176,12 +172,12 @@ fn overflowHandler( |
| 176 | 172 | |
| 177 | 173 | fn negationHandler( |
| 178 | 174 | data: *const OverflowData, |
| 179 | | old_value_handle: ValueHandle, |
| 175 | value_handle: ValueHandle, |
| 180 | 176 | ) callconv(.c) noreturn { |
| 181 | | const old_value = old_value_handle.getValue(data); |
| 177 | const value: Value = .{ .handle = value_handle, .td = data.td }; |
| 182 | 178 | logMessage( |
| 183 | 179 | "negation of {} cannot be represented in type {s}", |
| 184 | | .{ old_value, data.type_descriptor.getName() }, |
| 180 | .{ value, data.td.getName() }, |
| 185 | 181 | ); |
| 186 | 182 | } |
| 187 | 183 | |
| ... | ... | @@ -190,13 +186,13 @@ fn divRemHandler( |
| 190 | 186 | lhs_handle: ValueHandle, |
| 191 | 187 | rhs_handle: ValueHandle, |
| 192 | 188 | ) callconv(.c) noreturn { |
| 193 | | const lhs = lhs_handle.getValue(data); |
| 194 | | const rhs = rhs_handle.getValue(data); |
| 189 | const lhs: Value = .{ .handle = lhs_handle, .td = data.lhs_type }; |
| 190 | const rhs: Value = .{ .handle = rhs_handle, .td = data.rhs_type }; |
| 195 | 191 | |
| 196 | 192 | if (rhs.isMinusOne()) { |
| 197 | 193 | logMessage( |
| 198 | 194 | "division of {} by -1 cannot be represented in type {s}", |
| 199 | | .{ lhs, data.type_descriptor.getName() }, |
| 195 | .{ lhs, data.td.getName() }, |
| 200 | 196 | ); |
| 201 | 197 | } else logMessage("division by zero", .{}); |
| 202 | 198 | } |
| ... | ... | @@ -204,29 +200,30 @@ fn divRemHandler( |
| 204 | 200 | const AlignmentAssumptionData = extern struct { |
| 205 | 201 | loc: SourceLocation, |
| 206 | 202 | assumption_loc: SourceLocation, |
| 207 | | type_descriptor: *const TypeDescriptor, |
| 203 | td: *const TypeDescriptor, |
| 208 | 204 | }; |
| 209 | 205 | |
| 210 | 206 | fn alignmentAssumptionHandler( |
| 211 | 207 | data: *const AlignmentAssumptionData, |
| 212 | 208 | pointer: ValueHandle, |
| 213 | | alignment: ValueHandle, |
| 209 | alignment_handle: ValueHandle, |
| 214 | 210 | maybe_offset: ?ValueHandle, |
| 215 | 211 | ) callconv(.c) noreturn { |
| 216 | 212 | const real_pointer = @intFromPtr(pointer) - @intFromPtr(maybe_offset); |
| 217 | 213 | const lsb = @ctz(real_pointer); |
| 218 | 214 | const actual_alignment = @as(u64, 1) << @intCast(lsb); |
| 219 | | const mask = @intFromPtr(alignment) - 1; |
| 215 | const mask = @intFromPtr(alignment_handle) - 1; |
| 220 | 216 | const misalignment_offset = real_pointer & mask; |
| 217 | const alignment: Value = .{ .handle = alignment_handle, .td = data.td }; |
| 221 | 218 | |
| 222 | 219 | if (maybe_offset) |offset| { |
| 223 | 220 | logMessage( |
| 224 | 221 | "assumption of {} byte alignment (with offset of {} byte) for pointer of type {s} failed\n" ++ |
| 225 | 222 | "offset address is {} aligned, misalignment offset is {} bytes", |
| 226 | 223 | .{ |
| 227 | | alignment.getValue(data), |
| 224 | alignment, |
| 228 | 225 | @intFromPtr(offset), |
| 229 | | data.type_descriptor.getName(), |
| 226 | data.td.getName(), |
| 230 | 227 | actual_alignment, |
| 231 | 228 | misalignment_offset, |
| 232 | 229 | }, |
| ... | ... | @@ -236,8 +233,8 @@ fn alignmentAssumptionHandler( |
| 236 | 233 | "assumption of {} byte alignment for pointer of type {s} failed\n" ++ |
| 237 | 234 | "address is {} aligned, misalignment offset is {} bytes", |
| 238 | 235 | .{ |
| 239 | | alignment.getValue(data), |
| 240 | | data.type_descriptor.getName(), |
| 236 | alignment, |
| 237 | data.td.getName(), |
| 241 | 238 | actual_alignment, |
| 242 | 239 | misalignment_offset, |
| 243 | 240 | }, |
| ... | ... | @@ -256,8 +253,8 @@ fn shiftOob( |
| 256 | 253 | lhs_handle: ValueHandle, |
| 257 | 254 | rhs_handle: ValueHandle, |
| 258 | 255 | ) callconv(.c) noreturn { |
| 259 | | const lhs: Value = .{ .handle = lhs_handle, .type_descriptor = data.lhs_type }; |
| 260 | | const rhs: Value = .{ .handle = rhs_handle, .type_descriptor = data.rhs_type }; |
| 256 | const lhs: Value = .{ .handle = lhs_handle, .td = data.lhs_type }; |
| 257 | const rhs: Value = .{ .handle = rhs_handle, .td = data.rhs_type }; |
| 261 | 258 | |
| 262 | 259 | if (rhs.isNegative() or |
| 263 | 260 | rhs.getPositiveInteger() >= data.lhs_type.getIntegerSize()) |
| ... | ... | @@ -289,7 +286,7 @@ const OutOfBoundsData = extern struct { |
| 289 | 286 | }; |
| 290 | 287 | |
| 291 | 288 | fn outOfBounds(data: *const OutOfBoundsData, index_handle: ValueHandle) callconv(.c) noreturn { |
| 292 | | const index: Value = .{ .handle = index_handle, .type_descriptor = data.index_type }; |
| 289 | const index: Value = .{ .handle = index_handle, .td = data.index_type }; |
| 293 | 290 | logMessage( |
| 294 | 291 | "index {} out of bounds for type {s}", |
| 295 | 292 | .{ index, data.array_type.getName() }, |
| ... | ... | @@ -344,7 +341,7 @@ fn pointerOverflow( |
| 344 | 341 | |
| 345 | 342 | const TypeMismatchData = extern struct { |
| 346 | 343 | loc: SourceLocation, |
| 347 | | type_descriptor: *const TypeDescriptor, |
| 344 | td: *const TypeDescriptor, |
| 348 | 345 | log_alignment: u8, |
| 349 | 346 | kind: enum(u8) { |
| 350 | 347 | load, |
| ... | ... | @@ -388,17 +385,17 @@ fn typeMismatch( |
| 388 | 385 | if (pointer == null) { |
| 389 | 386 | logMessage( |
| 390 | 387 | "{s} null pointer of type {s}", |
| 391 | | .{ data.kind.getName(), data.type_descriptor.getName() }, |
| 388 | .{ data.kind.getName(), data.td.getName() }, |
| 392 | 389 | ); |
| 393 | 390 | } else if (!std.mem.isAligned(handle, alignment)) { |
| 394 | 391 | logMessage( |
| 395 | 392 | "{s} misaligned address 0x{x} for type {s}, which requires {} byte alignment", |
| 396 | | .{ data.kind.getName(), handle, data.type_descriptor.getName(), alignment }, |
| 393 | .{ data.kind.getName(), handle, data.td.getName(), alignment }, |
| 397 | 394 | ); |
| 398 | 395 | } else { |
| 399 | 396 | logMessage( |
| 400 | 397 | "{s} address 0x{x} with insufficient space for an object of type {s}", |
| 401 | | .{ data.kind.getName(), handle, data.type_descriptor.getName() }, |
| 398 | .{ data.kind.getName(), handle, data.td.getName() }, |
| 402 | 399 | ); |
| 403 | 400 | } |
| 404 | 401 | } |
| ... | ... | @@ -438,16 +435,18 @@ fn nonNullArg(data: *const NonNullArgData) callconv(.c) noreturn { |
| 438 | 435 | |
| 439 | 436 | const InvalidValueData = extern struct { |
| 440 | 437 | loc: SourceLocation, |
| 441 | | type_descriptor: *const TypeDescriptor, |
| 438 | td: *const TypeDescriptor, |
| 442 | 439 | }; |
| 443 | 440 | |
| 444 | 441 | fn loadInvalidValue( |
| 445 | 442 | data: *const InvalidValueData, |
| 446 | 443 | value_handle: ValueHandle, |
| 447 | 444 | ) callconv(.c) noreturn { |
| 448 | | logMessage("load of value {}, which is not valid for type {s}", .{ |
| 449 | | value_handle.getValue(data), data.type_descriptor.getName(), |
| 450 | | }); |
| 445 | const value: Value = .{ .handle = value_handle, .td = data.td }; |
| 446 | logMessage( |
| 447 | "load of value {}, which is not valid for type {s}", |
| 448 | .{ value, data.td.getName() }, |
| 449 | ); |
| 451 | 450 | } |
| 452 | 451 | |
| 453 | 452 | const InvalidBuiltinData = extern struct { |
| ... | ... | @@ -467,16 +466,18 @@ fn invalidBuiltin(data: *const InvalidBuiltinData) callconv(.c) noreturn { |
| 467 | 466 | |
| 468 | 467 | const VlaBoundNotPositive = extern struct { |
| 469 | 468 | loc: SourceLocation, |
| 470 | | type_descriptor: *const TypeDescriptor, |
| 469 | td: *const TypeDescriptor, |
| 471 | 470 | }; |
| 472 | 471 | |
| 473 | 472 | fn vlaBoundNotPositive( |
| 474 | 473 | data: *const VlaBoundNotPositive, |
| 475 | 474 | bound_handle: ValueHandle, |
| 476 | 475 | ) callconv(.c) noreturn { |
| 477 | | logMessage("variable length array bound evaluates to non-positive value {}", .{ |
| 478 | | bound_handle.getValue(data), |
| 479 | | }); |
| 476 | const bound: Value = .{ .handle = bound_handle, .td = data.td }; |
| 477 | logMessage( |
| 478 | "variable length array bound evaluates to non-positive value {}", |
| 479 | .{bound}, |
| 480 | ); |
| 480 | 481 | } |
| 481 | 482 | |
| 482 | 483 | const FloatCastOverflowData = extern struct { |
| ... | ... | @@ -499,13 +500,13 @@ fn floatCastOverflow( |
| 499 | 500 | const ptr: [*]const u8 = @ptrCast(data_handle); |
| 500 | 501 | if (@as(u16, ptr[0]) + @as(u16, ptr[1]) < 2 or ptr[0] == 0xFF or ptr[1] == 0xFF) { |
| 501 | 502 | const data: *const FloatCastOverflowData = @ptrCast(data_handle); |
| 502 | | const from_value: Value = .{ .handle = from_handle, .type_descriptor = data.from }; |
| 503 | const from_value: Value = .{ .handle = from_handle, .td = data.from }; |
| 503 | 504 | logMessage("{} is outside the range of representable values of type {s}", .{ |
| 504 | 505 | from_value, data.to.getName(), |
| 505 | 506 | }); |
| 506 | 507 | } else { |
| 507 | 508 | const data: *const FloatCastOverflowDataV2 = @ptrCast(data_handle); |
| 508 | | const from_value: Value = .{ .handle = from_handle, .type_descriptor = data.from }; |
| 509 | const from_value: Value = .{ .handle = from_handle, .td = data.from }; |
| 509 | 510 | logMessage("{} is outside the range of representable values of type {s}", .{ |
| 510 | 511 | from_value, data.to.getName(), |
| 511 | 512 | }); |