| ... | ... | @@ -432,8 +432,6 @@ pub const DeclGen = struct { |
| 432 | 432 | dg: *DeclGen, |
| 433 | 433 | /// Cached reference of the u32 type. |
| 434 | 434 | u32_ty_ref: CacheRef, |
| 435 | | /// Cached type id of the u32 type. |
| 436 | | u32_ty_id: IdRef, |
| 437 | 435 | /// The members of the resulting structure type |
| 438 | 436 | members: std.ArrayList(CacheRef), |
| 439 | 437 | /// The initializers of each of the members. |
| ... | ... | @@ -466,9 +464,7 @@ pub const DeclGen = struct { |
| 466 | 464 | } |
| 467 | 465 | |
| 468 | 466 | const word = @bitCast(Word, self.partial_word.buffer); |
| 469 | | const result_id = self.dg.spv.allocId(); |
| 470 | | // TODO: Integrate with caching mechanism |
| 471 | | try self.dg.spv.emitConstant(self.u32_ty_id, result_id, .{ .uint32 = word }); |
| 467 | const result_id = try self.dg.spv.constInt(self.u32_ty_ref, word); |
| 472 | 468 | try self.members.append(self.u32_ty_ref); |
| 473 | 469 | try self.initializers.append(result_id); |
| 474 | 470 | |
| ... | ... | @@ -519,11 +515,7 @@ pub const DeclGen = struct { |
| 519 | 515 | } |
| 520 | 516 | |
| 521 | 517 | fn addNullPtr(self: *@This(), ptr_ty_ref: CacheRef) !void { |
| 522 | | const result_id = self.dg.spv.allocId(); |
| 523 | | try self.dg.spv.sections.types_globals_constants.emit(self.dg.spv.gpa, .OpConstantNull, .{ |
| 524 | | .id_result_type = self.dg.typeId(ptr_ty_ref), |
| 525 | | .id_result = result_id, |
| 526 | | }); |
| 518 | const result_id = try self.dg.spv.constNull(ptr_ty_ref); |
| 527 | 519 | try self.addPtr(ptr_ty_ref, result_id); |
| 528 | 520 | } |
| 529 | 521 | |
| ... | ... | @@ -909,7 +901,6 @@ pub const DeclGen = struct { |
| 909 | 901 | var icl = IndirectConstantLowering{ |
| 910 | 902 | .dg = self, |
| 911 | 903 | .u32_ty_ref = u32_ty_ref, |
| 912 | | .u32_ty_id = self.typeId(u32_ty_ref), |
| 913 | 904 | .members = std.ArrayList(CacheRef).init(self.gpa), |
| 914 | 905 | .initializers = std.ArrayList(IdRef).init(self.gpa), |
| 915 | 906 | .decl_deps = std.AutoArrayHashMap(SpvModule.Decl.Index, void).init(self.gpa), |
| ... | ... | @@ -978,19 +969,12 @@ pub const DeclGen = struct { |
| 978 | 969 | /// This function should only be called during function code generation. |
| 979 | 970 | fn constant(self: *DeclGen, ty: Type, val: Value, repr: Repr) !IdRef { |
| 980 | 971 | const target = self.getTarget(); |
| 981 | | const section = &self.spv.sections.types_globals_constants; |
| 982 | 972 | const result_ty_ref = try self.resolveType(ty, repr); |
| 983 | | const result_ty_id = self.typeId(result_ty_ref); |
| 984 | 973 | |
| 985 | 974 | log.debug("constant: ty = {}, val = {}", .{ ty.fmt(self.module), val.fmtValue(ty, self.module) }); |
| 986 | 975 | |
| 987 | 976 | if (val.isUndef()) { |
| 988 | | const result_id = self.spv.allocId(); |
| 989 | | try section.emit(self.spv.gpa, .OpUndef, .{ |
| 990 | | .id_result_type = result_ty_id, |
| 991 | | .id_result = result_id, |
| 992 | | }); |
| 993 | | return result_id; |
| 977 | return self.spv.constUndef(result_ty_ref); |
| 994 | 978 | } |
| 995 | 979 | |
| 996 | 980 | switch (ty.zigTypeTag()) { |
| ... | ... | @@ -1002,28 +986,15 @@ pub const DeclGen = struct { |
| 1002 | 986 | } |
| 1003 | 987 | }, |
| 1004 | 988 | .Bool => switch (repr) { |
| 1005 | | .direct => { |
| 1006 | | const result_id = self.spv.allocId(); |
| 1007 | | const operands = .{ .id_result_type = result_ty_id, .id_result = result_id }; |
| 1008 | | if (val.toBool()) { |
| 1009 | | try section.emit(self.spv.gpa, .OpConstantTrue, operands); |
| 1010 | | } else { |
| 1011 | | try section.emit(self.spv.gpa, .OpConstantFalse, operands); |
| 1012 | | } |
| 1013 | | return result_id; |
| 1014 | | }, |
| 989 | .direct => return try self.spv.constBool(result_ty_ref, val.toBool()), |
| 1015 | 990 | .indirect => return try self.spv.constInt(result_ty_ref, @boolToInt(val.toBool())), |
| 1016 | 991 | }, |
| 1017 | | .Float => { |
| 1018 | | const result_id = self.spv.allocId(); |
| 1019 | | switch (ty.floatBits(target)) { |
| 1020 | | 16 => try self.spv.emitConstant(result_ty_id, result_id, .{ .float32 = val.toFloat(f16) }), |
| 1021 | | 32 => try self.spv.emitConstant(result_ty_id, result_id, .{ .float32 = val.toFloat(f32) }), |
| 1022 | | 64 => try self.spv.emitConstant(result_ty_id, result_id, .{ .float64 = val.toFloat(f64) }), |
| 1023 | | 80, 128 => unreachable, // TODO |
| 1024 | | else => unreachable, |
| 1025 | | } |
| 1026 | | return result_id; |
| 992 | .Float => return switch (ty.floatBits(target)) { |
| 993 | 16 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float16 = val.toFloat(f16) } } }), |
| 994 | 32 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float32 = val.toFloat(f32) } } }), |
| 995 | 64 => try self.spv.resolveId(.{ .float = .{ .ty = result_ty_ref, .value = .{ .float64 = val.toFloat(f64) } } }), |
| 996 | 80, 128 => unreachable, // TODO |
| 997 | else => unreachable, |
| 1027 | 998 | }, |
| 1028 | 999 | .ErrorSet => { |
| 1029 | 1000 | const value = switch (val.tag()) { |
| ... | ... | @@ -1081,7 +1052,7 @@ pub const DeclGen = struct { |
| 1081 | 1052 | try self.func.decl_deps.put(self.spv.gpa, spv_decl_index, {}); |
| 1082 | 1053 | |
| 1083 | 1054 | try self.func.body.emit(self.spv.gpa, .OpLoad, .{ |
| 1084 | | .id_result_type = result_ty_id, |
| 1055 | .id_result_type = self.typeId(result_ty_ref), |
| 1085 | 1056 | .id_result = result_id, |
| 1086 | 1057 | .pointer = self.spv.declPtr(spv_decl_index).result_id, |
| 1087 | 1058 | }); |
| ... | ... | @@ -2607,9 +2578,8 @@ pub const DeclGen = struct { |
| 2607 | 2578 | .Struct => switch (object_ty.containerLayout()) { |
| 2608 | 2579 | .Packed => unreachable, // TODO |
| 2609 | 2580 | else => { |
| 2610 | | const u32_ty_id = self.typeId(try self.intType(.unsigned, 32)); |
| 2611 | | const field_index_id = self.spv.allocId(); |
| 2612 | | try self.spv.emitConstant(u32_ty_id, field_index_id, .{ .uint32 = field_index }); |
| 2581 | const field_index_ty_ref = try self.intType(.unsigned, 32); |
| 2582 | const field_index_id = try self.spv.constInt(field_index_ty_ref, field_index); |
| 2613 | 2583 | const result_ty_ref = try self.resolveType(result_ptr_ty, .direct); |
| 2614 | 2584 | return try self.accessChain(result_ty_ref, object_ptr, &.{field_index_id}); |
| 2615 | 2585 | }, |