| ... | ... | @@ -19,6 +19,7 @@ const Module = @import("Module.zig"); |
| 19 | 19 | const spec = @import("spec.zig"); |
| 20 | 20 | const Opcode = spec.Opcode; |
| 21 | 21 | const IdResult = spec.IdResult; |
| 22 | const StorageClass = spec.StorageClass; |
| 22 | 23 | |
| 23 | 24 | const Self = @This(); |
| 24 | 25 | |
| ... | ... | @@ -54,9 +55,21 @@ const Tag = enum { |
| 54 | 55 | /// Array type |
| 55 | 56 | /// data is payload to ArrayType |
| 56 | 57 | type_array, |
| 57 | | /// Function (proto)type. |
| 58 | /// Function (proto)type |
| 58 | 59 | /// data is payload to FunctionType |
| 59 | 60 | type_function, |
| 61 | /// Pointer type in the CrossWorkgroup storage class |
| 62 | /// data is child type |
| 63 | type_ptr_generic, |
| 64 | /// Pointer type in the CrossWorkgroup storage class |
| 65 | /// data is child type |
| 66 | type_ptr_crosswgp, |
| 67 | /// Pointer type in the Function storage class |
| 68 | /// data is child type |
| 69 | type_ptr_function, |
| 70 | /// Simple pointer type that does not have any decorations. |
| 71 | /// data is SimplePointerType |
| 72 | type_ptr_simple, |
| 60 | 73 | |
| 61 | 74 | // -- Values |
| 62 | 75 | /// Value of type u8 |
| ... | ... | @@ -100,6 +113,11 @@ const Tag = enum { |
| 100 | 113 | return_type: Ref, |
| 101 | 114 | }; |
| 102 | 115 | |
| 116 | const SimplePointerType = struct { |
| 117 | storage_class: StorageClass, |
| 118 | child_type: Ref, |
| 119 | }; |
| 120 | |
| 103 | 121 | const Float64 = struct { |
| 104 | 122 | // Low-order 32 bits of the value. |
| 105 | 123 | low: u32, |
| ... | ... | @@ -182,6 +200,7 @@ pub const Key = union(enum) { |
| 182 | 200 | vector_type: VectorType, |
| 183 | 201 | array_type: ArrayType, |
| 184 | 202 | function_type: FunctionType, |
| 203 | ptr_type: PointerType, |
| 185 | 204 | |
| 186 | 205 | // -- values |
| 187 | 206 | int: Int, |
| ... | ... | @@ -210,6 +229,15 @@ pub const Key = union(enum) { |
| 210 | 229 | parameters: []const Ref, |
| 211 | 230 | }; |
| 212 | 231 | |
| 232 | pub const PointerType = struct { |
| 233 | storage_class: StorageClass, |
| 234 | child_type: Ref, |
| 235 | // TODO: Decorations: |
| 236 | // - Alignment |
| 237 | // - ArrayStride, |
| 238 | // - MaxByteOffset, |
| 239 | }; |
| 240 | |
| 213 | 241 | pub const Int = struct { |
| 214 | 242 | /// The type: any bitness integer. |
| 215 | 243 | ty: Ref, |
| ... | ... | @@ -406,6 +434,14 @@ fn emit( |
| 406 | 434 | section.writeOperand(IdResult, self.resultId(param_type)); |
| 407 | 435 | } |
| 408 | 436 | }, |
| 437 | .ptr_type => |ptr| { |
| 438 | try section.emit(spv.gpa, .OpTypePointer, .{ |
| 439 | .id_result = result_id, |
| 440 | .storage_class = ptr.storage_class, |
| 441 | .type = self.resultId(ptr.child_type), |
| 442 | }); |
| 443 | // TODO: Decorations? |
| 444 | }, |
| 409 | 445 | .int => |int| { |
| 410 | 446 | const int_type = self.lookup(int.ty).int_type; |
| 411 | 447 | const ty_id = self.resultId(int.ty); |
| ... | ... | @@ -491,6 +527,31 @@ pub fn resolve(self: *Self, spv: *Module, key: Key) !Ref { |
| 491 | 527 | .data = extra, |
| 492 | 528 | }; |
| 493 | 529 | }, |
| 530 | .ptr_type => |ptr| switch (ptr.storage_class) { |
| 531 | .Generic => Item{ |
| 532 | .tag = .type_ptr_generic, |
| 533 | .result_id = result_id, |
| 534 | .data = @enumToInt(ptr.child_type), |
| 535 | }, |
| 536 | .CrossWorkgroup => Item{ |
| 537 | .tag = .type_ptr_crosswgp, |
| 538 | .result_id = result_id, |
| 539 | .data = @enumToInt(ptr.child_type), |
| 540 | }, |
| 541 | .Function => Item{ |
| 542 | .tag = .type_ptr_function, |
| 543 | .result_id = result_id, |
| 544 | .data = @enumToInt(ptr.child_type), |
| 545 | }, |
| 546 | else => |storage_class| Item{ |
| 547 | .tag = .type_ptr_simple, |
| 548 | .result_id = result_id, |
| 549 | .data = try self.addExtra(spv, Tag.SimplePointerType{ |
| 550 | .storage_class = storage_class, |
| 551 | .child_type = ptr.child_type, |
| 552 | }), |
| 553 | }, |
| 554 | }, |
| 494 | 555 | .int => |int| blk: { |
| 495 | 556 | const int_type = self.lookup(int.ty).int_type; |
| 496 | 557 | if (int_type.signedness == .unsigned and int_type.bits == 8) { |
| ... | ... | @@ -599,6 +660,33 @@ pub fn lookup(self: *const Self, ref: Ref) Key { |
| 599 | 660 | }, |
| 600 | 661 | }; |
| 601 | 662 | }, |
| 663 | .type_ptr_generic => .{ |
| 664 | .ptr_type = .{ |
| 665 | .storage_class = .Generic, |
| 666 | .child_type = @intToEnum(Ref, data), |
| 667 | }, |
| 668 | }, |
| 669 | .type_ptr_crosswgp => .{ |
| 670 | .ptr_type = .{ |
| 671 | .storage_class = .CrossWorkgroup, |
| 672 | .child_type = @intToEnum(Ref, data), |
| 673 | }, |
| 674 | }, |
| 675 | .type_ptr_function => .{ |
| 676 | .ptr_type = .{ |
| 677 | .storage_class = .Function, |
| 678 | .child_type = @intToEnum(Ref, data), |
| 679 | }, |
| 680 | }, |
| 681 | .type_ptr_simple => { |
| 682 | const payload = self.extraData(Tag.SimplePointerType, data); |
| 683 | return .{ |
| 684 | .ptr_type = .{ |
| 685 | .storage_class = payload.storage_class, |
| 686 | .child_type = payload.child_type, |
| 687 | }, |
| 688 | }; |
| 689 | }, |
| 602 | 690 | .float16 => .{ .float = .{ |
| 603 | 691 | .ty = self.get(.{ .float_type = .{ .bits = 16 } }), |
| 604 | 692 | .value = .{ .float16 = @bitCast(f16, @intCast(u16, data)) }, |
| ... | ... | @@ -677,6 +765,7 @@ fn addExtraAssumeCapacity(self: *Self, extra: anytype) !u32 { |
| 677 | 765 | u32 => field_val, |
| 678 | 766 | i32 => @bitCast(u32, field_val), |
| 679 | 767 | Ref => @enumToInt(field_val), |
| 768 | StorageClass => @enumToInt(field_val), |
| 680 | 769 | else => @compileError("Invalid type: " ++ @typeName(field.type)), |
| 681 | 770 | }; |
| 682 | 771 | self.extra.appendAssumeCapacity(word); |
| ... | ... | @@ -697,6 +786,7 @@ fn extraDataTrail(self: Self, comptime T: type, offset: u32) struct { data: T, t |
| 697 | 786 | u32 => word, |
| 698 | 787 | i32 => @bitCast(i32, word), |
| 699 | 788 | Ref => @intToEnum(Ref, word), |
| 789 | StorageClass => @intToEnum(StorageClass, word), |
| 700 | 790 | else => @compileError("Invalid type: " ++ @typeName(field.type)), |
| 701 | 791 | }; |
| 702 | 792 | } |