Skip to content

Commit

Permalink
svm: rename to use self pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexicon226 committed Jan 15, 2025
1 parent 07eda86 commit d334218
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 40 deletions.
34 changes: 17 additions & 17 deletions src/svm/executable.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -466,37 +466,37 @@ 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);
}
};
}

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);
}
};
46 changes: 23 additions & 23 deletions src/svm/memory.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
}
Expand All @@ -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,
};
Expand All @@ -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));
}
};

Expand All @@ -80,29 +80,29 @@ 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,
},
};
}

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];
}
Expand All @@ -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;
}
Expand All @@ -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);
}
};
Expand Down

0 comments on commit d334218

Please sign in to comment.