diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-02-01 12:49:51 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-01 12:49:51 -0800 |
| commit | 102d9542203e52839156cb61efdfca8403a379a9 (patch) | |
| tree | 72daf0d9a1ac3680f2f1fbc6447a120dde9b52ee /src/codegen/spirv.zig | |
| parent | 06b29c854656a1f9320ee16024b1c3a6b78180a5 (diff) | |
| parent | 1055344673a87af39f2288bae069ec9403e6086d (diff) | |
| download | zig-102d9542203e52839156cb61efdfca8403a379a9.tar.gz zig-102d9542203e52839156cb61efdfca8403a379a9.zip | |
Merge pull request #7827 from Snektron/spirv-setup
Stage 2: SPIR-V setup
Diffstat (limited to 'src/codegen/spirv.zig')
| -rw-r--r-- | src/codegen/spirv.zig | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig new file mode 100644 index 0000000000..5a262de836 --- /dev/null +++ b/src/codegen/spirv.zig @@ -0,0 +1,51 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; + +const spec = @import("spirv/spec.zig"); +const Module = @import("../Module.zig"); +const Decl = Module.Decl; + +pub fn writeInstruction(code: *std.ArrayList(u32), instr: spec.Opcode, args: []const u32) !void { + const word_count = @intCast(u32, args.len + 1); + try code.append((word_count << 16) | @enumToInt(instr)); + try code.appendSlice(args); +} + +pub const SPIRVModule = struct { + next_id: u32 = 0, + free_id_list: std.ArrayList(u32), + + pub fn init(allocator: *Allocator) SPIRVModule { + return .{ + .free_id_list = std.ArrayList(u32).init(allocator), + }; + } + + pub fn deinit(self: *SPIRVModule) void { + self.free_id_list.deinit(); + } + + pub fn allocId(self: *SPIRVModule) u32 { + if (self.free_id_list.popOrNull()) |id| return id; + + defer self.next_id += 1; + return self.next_id; + } + + pub fn freeId(self: *SPIRVModule, id: u32) void { + if (id + 1 == self.next_id) { + self.next_id -= 1; + } else { + // If no more memory to append the id to the free list, just ignore it. + self.free_id_list.append(id) catch {}; + } + } + + pub fn idBound(self: *SPIRVModule) u32 { + return self.next_id; + } + + pub fn genDecl(self: SPIRVModule, id: u32, code: *std.ArrayList(u32), decl: *Decl) !void { + + } +}; |
