| ... | @@ -230,6 +230,7 @@ pub const DeclGen = struct { | ... | @@ -230,6 +230,7 @@ pub const DeclGen = struct { |
| 230 | /// Generate a constant representing `val`. | 230 | /// Generate a constant representing `val`. |
| 231 | /// TODO: Deduplication? | 231 | /// TODO: Deduplication? |
| 232 | fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!u32 { | 232 | fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!u32 { |
| | 233 | const target = self.module.getTarget(); |
| 233 | const code = &self.spv.types_globals_constants; | 234 | const code = &self.spv.types_globals_constants; |
| 234 | const result_id = self.spv.allocResultId(); | 235 | const result_id = self.spv.allocResultId(); |
| 235 | const result_type_id = try self.getOrGenType(ty); | 236 | const result_type_id = try self.getOrGenType(ty); |
| ... | @@ -250,11 +251,11 @@ pub const DeclGen = struct { | ... | @@ -250,11 +251,11 @@ pub const DeclGen = struct { |
| 250 | | 251 | |
| 251 | // f16 and f32 require one word of storage. f64 requires 2, low-order first. | 252 | // f16 and f32 require one word of storage. f64 requires 2, low-order first. |
| 252 | | 253 | |
| 253 | switch (val.tag()) { | 254 | switch (ty.floatBits(target)) { |
| 254 | .float_16 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u16, val.castTag(.float_16).?.data) }), | 255 | 16 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u16, val.toFloat(f16)) }), |
| 255 | .float_32 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u32, val.castTag(.float_32).?.data) }), | 256 | 32 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u32, val.toFloat(f32)) }), |
| 256 | .float_64 => { | 257 | 64 => { |
| 257 | const float_bits = @bitCast(u64, val.castTag(.float_64).?.data); | 258 | const float_bits = @bitCast(u64, val.toFloat(f64)); |
| 258 | try writeInstruction(code, .OpConstant, &[_]u32{ | 259 | try writeInstruction(code, .OpConstant, &[_]u32{ |
| 259 | result_type_id, | 260 | result_type_id, |
| 260 | result_id, | 261 | result_id, |
| ... | @@ -262,9 +263,9 @@ pub const DeclGen = struct { | ... | @@ -262,9 +263,9 @@ pub const DeclGen = struct { |
| 262 | @truncate(u32, float_bits >> 32), | 263 | @truncate(u32, float_bits >> 32), |
| 263 | }); | 264 | }); |
| 264 | }, | 265 | }, |
| 265 | .float_128 => unreachable, // Filtered out in the call to getOrGenType. | 266 | 128 => unreachable, // Filtered out in the call to getOrGenType. |
| 266 | // TODO: What tags do we need to handle here anyway? | 267 | // TODO: Insert case for long double when the layout for that is determined. |
| 267 | else => return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: float constant generation of value {s}\n", .{val.tag()}), | 268 | else => unreachable, |
| 268 | } | 269 | } |
| 269 | }, | 270 | }, |
| 270 | else => return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: constant generation of type {s}\n", .{ty.zigTypeTag()}), | 271 | else => return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: constant generation of type {s}\n", .{ty.zigTypeTag()}), |