about summary refs log tree commit diff
path: root/synth.zig
diff options
context:
space:
mode:
Diffstat (limited to 'synth.zig')
-rw-r--r--synth.zig123
1 files changed, 50 insertions, 73 deletions
diff --git a/synth.zig b/synth.zig
index 453cb46..4e74cd8 100644
--- a/synth.zig
+++ b/synth.zig
@@ -16,83 +16,54 @@
 // 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 ArenaAllocator = std.heap.ArenaAllocator;
-const Dir = std.fs.Dir;
-const File = std.fs.File;
-const Reader = std.Io.Reader;
+const CompareOperator = std.math.CompareOperator;
+const Writer = std.io.Writer;
 const argsAlloc = std.process.argsAlloc;
 const argsFree = std.process.argsFree;
 const assert = std.debug.assert;
-const cwd = std.fs.cwd;
+const compare = std.math.compare;
+const enums = std.enums;
 const exit = std.process.exit;
 const page_allocator = std.heap.page_allocator;
 const parseUnsigned = std.fmt.parseUnsigned;
 const print = std.debug.print;
 const std = @import("std");
 
-const State = extern struct {
-    flags: u16,
-    r15: i64,
-    r14: i64,
-    r13: i64,
-    r12: i64,
-    r11: i64,
-    r10: i64,
-    r9: i64,
-    r8: i64,
-    rdi: i64,
-    rsi: i64,
-    rbp: i64,
-    rbx: i64,
-    rdx: i64,
-    rcx: i64,
-    rax: i64,
-    rsp: u64, // internal use only
-    rip: i64,
-};
-
-fn countFiles(dir: Dir) Dir.Iterator.Error!usize {
-    var count: usize = 0;
-    var entries = dir.iterate();
-    while (try entries.next()) |entry| {
-        assert(entry.kind == .file);
-        count += 1;
-    }
-    return count;
-}
-
-const ReadError = Dir.Iterator.Error || File.OpenError || Reader.Error;
+const RegisterEnum = Variables.RegisterEnum;
+const Variables = @import("Variables.zig");
+const signed_integers = Variables.signed_integers;
 
-const Data = struct {
-    states: []const State,
-    memory: []const u8,
+const Variable = RegisterEnum;
+const Expression = Variable;
+const Comparison = struct {
+    lhs: Expression,
+    op: CompareOperator,
+    rhs: Expression,
 
-    fn init(allocator: Allocator, dir: Dir,
-            stack_len: u64) (Allocator.Error || ReadError)!Data {
-        const count = try countFiles(dir);
-        const states = try allocator.alloc(State, count);
-        const memory = try allocator.alloc(u8, stack_len * count);
-        var entries = dir.iterate();
-        var sp: u64 = 0;
-        for (states) |*state| {
-            const entry = try entries.next();
-            const file = try dir.openFile(entry.?.name, .{});
-            defer file.close();
-            var buffer: [4096]u8 = undefined;
-            var reader = file.reader(&buffer);
-            assert(try reader.read(@ptrCast(state)) == @sizeOf(State));
-            state.rsp = sp;
-            assert(try reader.read(memory[sp..][0..stack_len]) == stack_len);
-            sp += stack_len;
-            print("{}\n", .{ state });
-        }
-        return .{ .states = states, .memory = memory };
+    fn check(cmp: Comparison, bot: Variables, top: Variables) !bool {
+        for (try bot.register(cmp.lhs), try bot.register(cmp.rhs)) |lhs, rhs|
+            if (compare(lhs, cmp.op, rhs))
+                return false;
+        for (try top.register(cmp.lhs), try top.register(cmp.rhs)) |lhs, rhs|
+            if (!compare(lhs, cmp.op, rhs))
+                return false;
+        return true;
     }
 
-    fn deinit(data: Data, allocator: Allocator) void {
-        allocator.free(data.states);
-        allocator.free(data.memory);
+    pub fn format(cmp: Comparison, writer: *Writer) Writer.Error!void {
+        try writer.print("{s}{f}{f}", .{
+            switch (cmp.op) {
+                .lt => "<",
+                .lte => "<=",
+                .eq => "==",
+                .gte => ">=",
+                .gt => ">",
+                .neq => "!=",
+            },
+            cmp.lhs,
+            cmp.rhs,
+        });
     }
 };
 
@@ -103,17 +74,23 @@ pub fn main() !void {
     const args = try argsAlloc(allocator);
     defer argsFree(allocator, args);
     if (args.len != 4) {
-        print("Usage: taosc-synth OFF-DIR ON-DIR STACK-SIZE", .{});
+        print("Usage: taosc-synth STACK_SIZE BOTTOM_DIR TOP_DIR", .{});
         exit(1);
     }
-    var off_dir = try cwd().openDir(args[1], .{ .iterate = true });
-    defer off_dir.close();
-    var on_dir = try cwd().openDir(args[2], .{ .iterate = true });
-    defer on_dir.close();
-    const stack_len = try parseUnsigned(u64, args[3], 0);
+    const stack_size = try parseUnsigned(usize, args[1], 0);
+    inline for (signed_integers) |Int|
+        assert(stack_size % @sizeOf(Int) == 0);
+    const bot = try Variables.init(allocator, stack_size, args[2]);
+    defer bot.deinit(allocator);
+    const top = try Variables.init(allocator, stack_size, args[3]);
+    defer top.deinit(allocator);
 
-    const off_data = try Data.init(allocator, off_dir, stack_len);
-    defer off_data.deinit(allocator);
-    const on_data = try Data.init(allocator, on_dir, stack_len);
-    defer on_data.deinit(allocator);
+    for (enums.values(RegisterEnum)) |lhs|
+        for (enums.values(RegisterEnum)) |rhs|
+            if (@intFromEnum(lhs) < @intFromEnum(rhs))
+                for (enums.values(CompareOperator)) |op| {
+                    const cmp = Comparison{ .lhs = lhs, .op = op, .rhs = rhs };
+                    if (try cmp.check(bot, top))
+                        print("{f}\n", .{ cmp });
+                };
 }