about summary refs log tree commit diff
path: root/Variables.zig
blob: d23f1f43f65842e41c5f190ff83e2a1d90544511 (plain) (blame)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! Variables captured at patch location
// Copyright (C) 2025  Nguyễn Gia Phong
//
// This file is part of taosc.
//
// Taosc is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Taosc is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with taosc.  If not, see <https://www.gnu.org/licenses/>.

const Allocator = std.mem.Allocator;
const Writer = std.io.Writer;
const assert = std.debug.assert;
const bytesAsSlice = std.mem.bytesAsSlice;
const comptimePrint = std.fmt.comptimePrint;
const cwd = std.fs.cwd;
const divCeil = std.math.divCeil;
const eql = std.meta.eql;
const expect = std.testing.expect;
const maxInt = std.math.maxInt;
const minInt = std.math.minInt;
const std = @import("std");
const tags = std.meta.tags;

pub const RegisterEnum = enum(u4) {
    rax, rbx, rcx, rdx, rsi, rdi, rsp, rbp,
    r8, r9, r10, r11, r12, r13, r14, r15,
};
pub const Register = i64;
const Registers = [tags(RegisterEnum).len]Register;

pub const signed_integers = .{ i64, i32, i16, i8 };
const alignment = @alignOf(Register);

test alignment {
    inline for (signed_integers) |Int|
        try expect(alignment >= @alignOf(Int));
}

fn alignedSize(T: type, count: usize) !usize {
    return try divCeil(usize, @sizeOf(T) * count, @alignOf(T)) * @alignOf(T);
}

fn packedSize(T: type, container_size: usize, count: usize) !usize {
    return @divExact(container_size, @sizeOf(T)) * try alignedSize(T, count);
}

const Variables = @This();
bytes: []align(alignment) const u8,
samples: usize,

pub fn init(allocator: Allocator, stack_size: usize,
            path: []const u8) !Variables {
    var dir = try cwd().openDir(path, .{ .iterate = true });
    defer dir.close();
    var entries = dir.iterate();
    var count: usize = 0;
    while (try entries.next()) |entry| {
        assert(entry.kind == .file);
        count += 1;
    }
    var size = try packedSize(Register, @sizeOf(Registers), count);
    inline for (signed_integers) |Int|
        size += try packedSize(Int, stack_size, count);
    const bytes = try allocator.alignedAlloc(u8,
        .fromByteUnits(alignment), size);
    errdefer allocator.free(bytes);

    const file_size = @sizeOf(Registers) + stack_size;
    const buffer = try allocator.alignedAlloc(u8,
        .fromByteUnits(@alignOf(Registers)), file_size);
    defer allocator.free(buffer);
    const registers: *Registers = @ptrCast(buffer.ptr);
    entries.reset();
    for (0..count) |index| {
        const entry = try entries.next();
        const file = try dir.openFile(entry.?.name, .{});
        defer file.close();
        assert(try file.getEndPos() == file_size);
        assert(try file.read(buffer) == file_size);
        var offset: usize = 0;
        inline for (registers) |register| {
            const slice = bytesAsSlice(Register, bytes[offset..]);
            slice[index] = register;
            offset += try alignedSize(Register, count);
        }
        inline for (signed_integers) |Int| {
            const aligned_size = try alignedSize(Int, count);
            for (bytesAsSlice(Int, buffer[@sizeOf(Registers)..])) |value| {
                const slice = bytesAsSlice(Int, bytes[offset..]);
                slice[index] = value;
                offset += aligned_size;
            }
        }
    }
    return .{ .bytes = bytes, .samples = count };
}

pub fn deinit(variables: Variables, allocator: Allocator) void {
    allocator.free(variables.bytes);
}

const ConstantEnum = enum(i64) {
    max1 = maxInt(i1),
    min2 = minInt(i2),
    max2 = maxInt(i2),
    min3 = minInt(i3),
    max3 = maxInt(i3),
    min4 = minInt(i4),
    max4 = maxInt(i4),
    min5 = minInt(i5),
    max5 = maxInt(i5),
    min6 = minInt(i6),
    max6 = maxInt(i6),
    min7 = minInt(i7),
    max7 = maxInt(i7),
    min8 = minInt(i8),
    max8 = maxInt(i8),
    min9 = minInt(i9),
    min16 = minInt(i16),
    max16 = maxInt(i16),
    min17 = minInt(i17),
    min32 = minInt(i32),
    max32 = maxInt(i32),
    min33 = minInt(i33),
    min64 = minInt(i64),
    max64 = maxInt(i64),
};

pub const Query = union(enum) {
    constant: ConstantEnum,
    register: RegisterEnum,
    stack: usize,

    pub fn skip(q: Query, r: Query, s: Query) bool {
        return eql(r, s)
            or r == .constant and s == .constant
            or (r == .constant or s == .constant) and q.constant != .max1;
    }

    pub fn format(query: Query, writer: *Writer) Writer.Error!void {
        switch (query) {
            inline .constant, .register => |tag|
                try writer.print("{s}", .{ @tagName(tag) }),
            .stack => unreachable,
        }
    }
};

pub fn constantQueries() []Query {
    var queries: [tags(ConstantEnum).len]Query = undefined;
    for (&queries, tags(ConstantEnum)) |*dest, src|
        dest.* = .{ .constant = src };
    return &queries;
}

pub fn allQueries(allocator: Allocator) Allocator.Error![]Query {
    const constants = tags(ConstantEnum);
    const registers = tags(RegisterEnum);
    const n = constants.len + registers.len;
    const queries = try allocator.alloc(Query, n);
    for (queries[0..constants.len], constants) |*dest, src|
        dest.* = .{ .constant = src };
    for (queries[constants.len..][0..registers.len],
         registers) |*dest, src|
        dest.* = .{ .register = src };
    return queries;
}

pub fn get(variables: Variables, query: Query,
           tmp: []Register) ![]const Register {
    switch (query) {
        .constant => |constant| switch (constant) {
            inline else => |tag| {
                for (tmp) |*register|
                    register.* = @intFromEnum(tag);
                return tmp;
            },
        },
        .register => |tag| {
            const aligned_size = try alignedSize(Register, variables.samples);
            const offset = aligned_size * @intFromEnum(tag);
            const slice = bytesAsSlice(Register, variables.bytes[offset..]);
            return @alignCast(slice[0..variables.samples]);
        },
        .stack => unreachable,
    }
}