Skip to content

Commit

Permalink
Compile on Linux?
Browse files Browse the repository at this point in the history
  • Loading branch information
Senryoku committed May 23, 2024
1 parent 7752a1c commit 2447cb0
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 27 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/sh4_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
with:
repository: SingleStepTests/sh4
path: ~/dev/sh4_json/
- name: Move JSON test files
run: mv /home/runner/work/Deecy/Deecy/~/dev/sh4_json ../SingleStepTests_sh4/
- name: Transcode JSON test files
working-directory: ~/dev/sh4_json/
working-directory: ../SingleStepTests_sh4/
run: python ./transcode_json.py
- name: Move JSON test files
run: find . -name "~/dev/sh4_json/*.json" -exec mv "{}" ../SingleStepTests_sh4/ \;
- uses: goto-bus-stop/setup-zig@v2
with:
version: 0.12.0-dev.3180+83e578a18
Expand Down
47 changes: 36 additions & 11 deletions src/jit/arm_jit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ const BlockCache = struct {

pub fn deinit(self: *@This()) void {
self._allocator.free(self.buffer);
std.os.windows.VirtualFree(self.blocks.ptr, 0, std.os.windows.MEM_RELEASE);

self.deallocate_blocks();
}

pub fn signal_write(self: *@This(), addr: u32) void {
Expand All @@ -67,25 +68,49 @@ const BlockCache = struct {
}

fn allocate_blocks(self: *@This()) !void {
if (@import("builtin").os.tag != .windows) {
@compileError("Unsupported OS - Use mmap on Linux here.");
switch (@import("builtin").os.tag) {
.windows => {
const blocks = try std.os.windows.VirtualAlloc(
null,
@sizeOf(?BasicBlock) * BlockEntryCount,
std.os.windows.MEM_RESERVE | std.os.windows.MEM_COMMIT,
std.os.windows.PAGE_READWRITE,
);
self.blocks = @as([*]?BasicBlock, @alignCast(@ptrCast(blocks)))[0..BlockEntryCount];
},
.linux => {
const blocks = try std.posix.mmap(
null,
@sizeOf(?BasicBlock) * BlockEntryCount,
std.posix.PROT.READ | std.posix.PROT.WRITE | std.posix.PROT.EXEC,
.{ .TYPE = .PRIVATE, .EXECUTABLE = true },
-1,
0,
);
self.blocks = @as([*]?BasicBlock, @alignCast(@ptrCast(blocks)))[0..BlockEntryCount];
},
else => @compileError("Unsupported OS."),
}
}

const blocks = try std.os.windows.VirtualAlloc(
null,
@sizeOf(?BasicBlock) * BlockEntryCount,
std.os.windows.MEM_RESERVE | std.os.windows.MEM_COMMIT,
std.os.windows.PAGE_READWRITE,
);
self.blocks = @as([*]?BasicBlock, @alignCast(@ptrCast(blocks)))[0..BlockEntryCount];
fn deallocate_blocks(self: *@This()) void {
switch (@import("builtin").os.tag) {
.windows => {
std.os.windows.VirtualFree(self.blocks.ptr, 0, std.os.windows.MEM_RELEASE);
},
.linux => {
std.posix.munmap(@as([*]align(std.mem.page_size) const u8, @alignCast(@ptrCast(self.blocks.ptr)))[0 .. self.blocks.len * @sizeOf(?BasicBlock)]);
},
else => @compileError("Unsupported OS."),
}
}

pub fn reset(self: *@This()) !void {
arm_jit_log.info(termcolor.blue("Resetting block cache."), .{});

self.cursor = 0;

std.os.windows.VirtualFree(self.blocks.ptr, 0, std.os.windows.MEM_RELEASE);
self.deallocate_blocks();
try self.allocate_blocks();

self.min_address = std.math.maxInt(u32);
Expand Down
49 changes: 36 additions & 13 deletions src/jit/sh4_jit.zig
Original file line number Diff line number Diff line change
Expand Up @@ -50,29 +50,52 @@ const BlockCache = struct {
pub fn deinit(self: *@This()) void {
self._allocator.free(self.buffer);

std.os.windows.VirtualFree(self.blocks.ptr, 0, std.os.windows.MEM_RELEASE);
self.deallocate_blocks();
}

fn allocate_blocks(self: *@This()) !void {
if (@import("builtin").os.tag != .windows) {
@compileError("Unsupported OS - Use mmap on Linux here.");
switch (@import("builtin").os.tag) {
.windows => {
const blocks = try std.os.windows.VirtualAlloc(
null,
@sizeOf(?BasicBlock) * BlockEntryCount,
std.os.windows.MEM_RESERVE | std.os.windows.MEM_COMMIT,
std.os.windows.PAGE_READWRITE,
);
self.blocks = @as([*]?BasicBlock, @alignCast(@ptrCast(blocks)))[0..BlockEntryCount];
},
.linux => {
const blocks = try std.posix.mmap(
null,
@sizeOf(?BasicBlock) * BlockEntryCount,
std.posix.PROT.READ | std.posix.PROT.WRITE | std.posix.PROT.EXEC,
.{ .TYPE = .PRIVATE, .EXECUTABLE = true },
-1,
0,
);
self.blocks = @as([*]?BasicBlock, @alignCast(@ptrCast(blocks)))[0..BlockEntryCount];
},
else => @compileError("Unsupported OS."),
}
}

const blocks = try std.os.windows.VirtualAlloc(
null,
@sizeOf(?BasicBlock) * BlockEntryCount,
std.os.windows.MEM_RESERVE | std.os.windows.MEM_COMMIT,
std.os.windows.PAGE_READWRITE,
);
self.blocks = @as([*]?BasicBlock, @alignCast(@ptrCast(blocks)))[0..BlockEntryCount];
fn deallocate_blocks(self: *@This()) void {
switch (@import("builtin").os.tag) {
.windows => {
// FIXME: Can I merely decommit and re-commit it?
std.os.windows.VirtualFree(self.blocks.ptr, 0, std.os.windows.MEM_RELEASE);
},
.linux => {
std.posix.munmap(@as([*]align(std.mem.page_size) const u8, @alignCast(@ptrCast(self.blocks.ptr)))[0 .. self.blocks.len * @sizeOf(?BasicBlock)]);
},
else => @compileError("Unsupported OS."),
}
}

pub fn reset(self: *@This()) !void {
self.cursor = 0;

// FIXME: Can I merely decommit and re-commit it?
std.os.windows.VirtualFree(self.blocks.ptr, 0, std.os.windows.MEM_RELEASE);

self.deallocate_blocks();
try self.allocate_blocks();
}

Expand Down

0 comments on commit 2447cb0

Please sign in to comment.