diff --git a/src/svm/executable.zig b/src/svm/executable.zig index e9540942c..70db6a042 100644 --- a/src/svm/executable.zig +++ b/src/svm/executable.zig @@ -344,11 +344,11 @@ pub const Assembler = struct { }; } - fn tokenize(assembler: *Assembler, allocator: std.mem.Allocator) ![]const Statement { + fn tokenize(self: *Assembler, allocator: std.mem.Allocator) ![]const Statement { var statements: std.ArrayListUnmanaged(Statement) = .{}; defer statements.deinit(allocator); - var lines = std.mem.splitScalar(u8, assembler.source, '\n'); + var lines = std.mem.splitScalar(u8, self.source, '\n'); while (lines.next()) |line| { if (line.len == 0) continue; // empty line, skip @@ -429,13 +429,13 @@ pub fn Registry(T: type) type { /// Duplicates `name` to free later. fn register( - registry: *Self, + self: *Self, allocator: std.mem.Allocator, key: u32, name: []const u8, value: T, ) !void { - const gop = try registry.map.getOrPut(allocator, key); + const gop = try self.map.getOrPut(allocator, key); if (gop.found_existing) { if (!std.mem.eql(u8, gop.value_ptr.name, name)) { return error.SymbolHashCollision; @@ -446,18 +446,18 @@ pub fn Registry(T: type) type { } pub fn registerHashed( - registry: *Self, + self: *Self, allocator: std.mem.Allocator, name: []const u8, value: T, ) !u32 { const key = sbpf.hashSymbolName(name); - try registry.register(allocator, key, name, value); + try self.register(allocator, key, name, value); return key; } pub fn registerHashedLegacy( - registry: *Self, + self: *Self, allocator: std.mem.Allocator, name: []const u8, value: T, @@ -466,29 +466,29 @@ pub fn Registry(T: type) type { sbpf.hashSymbolName(name) else sbpf.hashSymbolName(&std.mem.toBytes(value)); - try registry.register(allocator, hash, &.{}, value); + try self.register(allocator, hash, &.{}, value); return hash; } - pub fn lookupKey(registry: *const Self, key: u32) ?Entry { - return registry.map.get(key); + pub fn lookupKey(self: *const Self, key: u32) ?Entry { + return self.map.get(key); } // TODO: this can be sped up by using a bidirectional map - pub fn lookupName(registry: *const Self, name: []const u8) ?Entry { - var iter = registry.map.valueIterator(); + pub fn lookupName(self: *const Self, name: []const u8) ?Entry { + var iter = self.map.valueIterator(); while (iter.next()) |entry| { if (std.mem.eql(u8, entry.name, name)) return entry.*; } return null; } - pub fn deinit(registry: *Self, allocator: std.mem.Allocator) void { - var iter = registry.map.valueIterator(); + pub fn deinit(self: *Self, allocator: std.mem.Allocator) void { + var iter = self.map.valueIterator(); while (iter.next()) |entry| { allocator.free(entry.name); } - registry.map.deinit(allocator); + self.map.deinit(allocator); } }; } @@ -496,7 +496,7 @@ pub fn Registry(T: type) type { pub const BuiltinProgram = struct { functions: Registry(*const fn (*Vm) syscalls.Error!void) = .{}, - pub fn deinit(program: *BuiltinProgram, allocator: std.mem.Allocator) void { - program.functions.deinit(allocator); + pub fn deinit(self: *BuiltinProgram, allocator: std.mem.Allocator) void { + self.functions.deinit(allocator); } }; diff --git a/src/svm/memory.zig b/src/svm/memory.zig index 7d49fdb5b..2fac51f8d 100644 --- a/src/svm/memory.zig +++ b/src/svm/memory.zig @@ -15,19 +15,19 @@ pub const MemoryMap = union(enum) { return .{ .aligned = try AlignedMemoryMap.init(regions, version) }; } - pub fn region(map: MemoryMap, vm_addr: u64) !Region { - return switch (map) { + pub fn region(self: MemoryMap, vm_addr: u64) !Region { + return switch (self) { .aligned => |aligned| aligned.region(vm_addr), }; } pub fn vmap( - map: MemoryMap, + self: MemoryMap, comptime state: MemoryState, vm_addr: u64, len: u64, ) !state.Slice() { - return switch (map) { + return switch (self) { .aligned => |aligned| aligned.vmap(state, vm_addr, len), }; } @@ -37,15 +37,15 @@ pub const MemoryState = enum { mutable, constant, - fn Slice(state: MemoryState) type { - return switch (state) { + fn Slice(self: MemoryState) type { + return switch (self) { .constant => []const u8, .mutable => []u8, }; } - fn Many(access: MemoryState) type { - return switch (access) { + fn Many(self: MemoryState) type { + return switch (self) { .constant => [*]const u8, .mutable => [*]u8, }; @@ -56,9 +56,9 @@ const HostMemory = union(MemoryState) { mutable: []u8, constant: []const u8, - fn getSlice(host: HostMemory, comptime state: MemoryState) !state.Slice() { - if (host != state) return error.AccessViolation; - return @field(host, @tagName(state)); + fn getSlice(self: HostMemory, comptime state: MemoryState) !state.Slice() { + if (self != state) return error.AccessViolation; + return @field(self, @tagName(state)); } }; @@ -80,13 +80,13 @@ pub const Region = struct { /// Get the underlying host slice of memory. /// /// Returns an error if you're trying to get mutable access to a constant region. - pub fn getSlice(reg: Region, comptime state: MemoryState) !state.Slice() { + pub fn getSlice(self: Region, comptime state: MemoryState) !state.Slice() { return switch (state) { - .constant => switch (reg.host_memory) { + .constant => switch (self.host_memory) { .constant => |constant| constant, .mutable => |mutable| mutable, }, - .mutable => switch (reg.host_memory) { + .mutable => switch (self.host_memory) { .constant => return error.AccessViolation, .mutable => |mutable| mutable, }, @@ -94,15 +94,15 @@ pub const Region = struct { } fn translate( - reg: Region, + self: Region, comptime state: MemoryState, vm_addr: u64, len: u64, ) !state.Slice() { - if (vm_addr < reg.vm_addr_start) return error.InvalidVirtualAddress; + if (vm_addr < self.vm_addr_start) return error.InvalidVirtualAddress; - const host_slice = try reg.getSlice(state); - const begin_offset = vm_addr -| reg.vm_addr_start; + const host_slice = try self.getSlice(state); + const begin_offset = vm_addr -| self.vm_addr_start; if (begin_offset + len <= host_slice.len) { return host_slice[begin_offset..][0..len]; } @@ -128,11 +128,11 @@ const AlignedMemoryMap = struct { }; } - fn region(map: *const AlignedMemoryMap, vm_addr: u64) !Region { + fn region(self: *const AlignedMemoryMap, vm_addr: u64) !Region { const index = vm_addr >> VIRTUAL_ADDRESS_BITS; - if (index >= 1 and index <= map.regions.len) { - const reg = map.regions[index - 1]; + if (index >= 1 and index <= self.regions.len) { + const reg = self.regions[index - 1]; if (vm_addr >= reg.vm_addr_start and vm_addr < reg.vm_addr_end) { return reg; } @@ -142,12 +142,12 @@ const AlignedMemoryMap = struct { } fn vmap( - map: *const AlignedMemoryMap, + self: *const AlignedMemoryMap, comptime state: MemoryState, vm_addr: u64, len: u64, ) !state.Slice() { - const reg = try map.region(vm_addr); + const reg = try self.region(vm_addr); return reg.translate(state, vm_addr, len); } };