diff options
Diffstat (limited to 'src/codegen/spirv.zig')
| -rw-r--r-- | src/codegen/spirv.zig | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 2fe759dc03..5a262de836 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -1,4 +1,6 @@ const std = @import("std"); +const Allocator = std.mem.Allocator; + const spec = @import("spirv/spec.zig"); const Module = @import("../Module.zig"); const Decl = Module.Decl; @@ -10,14 +12,35 @@ pub fn writeInstruction(code: *std.ArrayList(u32), instr: spec.Opcode, args: []c } pub const SPIRVModule = struct { - // TODO: Also use a free list. 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; } |
