aboutsummaryrefslogtreecommitdiff
path: root/src/codegen/spirv.zig
blob: 23fc45616fa6ef19797f3b88090caac97a23710a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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 {}
};