Compare commits

..

No commits in common. "bd19a43c0d29d8cea4bb76eb45c0832ed7105bb8" and "d26f11aec4616ca8b0d7ded509dc923eb4a8ac26" have entirely different histories.

5 changed files with 22 additions and 73 deletions

3
gdb.sh
View File

@ -1,3 +0,0 @@
#!/bin/sh
gdb ./zig-out/bin/nmvm -ex 'b arch.x86-64.execute' -ex 'layout asm' -ex 'r'

View File

@ -1,8 +1,2 @@
# .nmvm Near Metal Virtual Machine # .nmvm Near Metal Virtual Machine
Exercise in building low overhead VM via architecture specific means. Exercise in building low overhead VM via architecture specific means.
## Cases
- Native stack usage for virtual machine.
- Specific permutations of instructions for case optimizations.
- Array processing instructions.
- Absolute addressing for interpreter state, in TLS.

View File

@ -1,10 +1,5 @@
// todo: Use r12, r13, ... instead? They're preserved in Sys V abi which might make it more confortable,
// but they might increase binary size, gotta test.
// todo: Try using something else that has lesser opcode size.
// Execution thread convention: // Execution thread convention:
// r12 <- binary thread pointer // rdi <- binary thread
// r13 <- return stack pointer
// Resources used: // Resources used:
// https://mort.coffee/home/fast-interpreters/ // https://mort.coffee/home/fast-interpreters/
@ -15,17 +10,14 @@
// https://csiflabs.cs.ucdavis.edu/~ssdavis/50/att-syntax.htm // https://csiflabs.cs.ucdavis.edu/~ssdavis/50/att-syntax.htm
pub const Word = u64; pub const Word = u64;
pub const RecursionLimit = 1024;
threadlocal var return_stack: [RecursionLimit + 1]Word = undefined;
// todo: Variant that pushes array of words. // todo: Variant that pushes array of words.
/// (iw | -- iw) /// (iw | -- iw)
pub fn opPushWord() callconv(.Naked) noreturn { pub fn opPushWord() callconv(.Naked) noreturn {
asm volatile ( asm volatile (
\\ add $0x10, %%r12 \\ add $0x10, %%rdi
\\ pushq -8(%%r12) \\ pushq -8(%%rdi)
\\ jmpq *(%%r12) \\ jmpq *(%%rdi)
); );
} }
@ -33,9 +25,9 @@ pub fn opPushWord() callconv(.Naked) noreturn {
/// (w --) /// (w --)
pub fn opSinkWord() callconv(.Naked) noreturn { pub fn opSinkWord() callconv(.Naked) noreturn {
asm volatile ( asm volatile (
\\ add $0x08, %%r12 \\ add $0x08, %%rdi
\\ addq $0x08, %%rsp \\ addq $0x08, %%rsp
\\ jmpq *(%%r12) \\ jmpq *(%%rdi)
); );
} }
@ -61,13 +53,13 @@ pub fn opSumWordsWithOverflow() callconv(.Naked) noreturn {
// https://www.felixcloutier.com/x86/setcc // https://www.felixcloutier.com/x86/setcc
// idea: Could https://www.felixcloutier.com/x86/cmovcc be better for overflow push? // idea: Could https://www.felixcloutier.com/x86/cmovcc be better for overflow push?
asm volatile ( asm volatile (
\\ addq $0x08, %%r12
\\ movq (%%rsp), %%rax \\ movq (%%rsp), %%rax
\\ adcq 8(%%rsp), %%rax \\ adcq 8(%%rsp), %%rax
\\ movq %%rax, 8(%%rsp) \\ movq %%rax, 8(%%rsp)
\\ setc %%al \\ setc %%al
\\ movb %%al, (%%rsp) \\ movb %%al, 7(%%rsp)
\\ jmpq *(%%r12) \\ addq $0x08, %%rdi
\\ jmpq *(%%rdi)
); );
} }
@ -80,47 +72,20 @@ pub fn opSumWordsWithOverflow() callconv(.Naked) noreturn {
// @call(.always_tail, binary[offset].function, .{ &binary[offset], cond }); // @call(.always_tail, binary[offset].function, .{ &binary[offset], cond });
// } // }
// todo: Complex call op that would receive immediate mask that would tell
// which positions of stack to duplicate, as well as mixing of plain immediate operands.
// Or we could decouple it from call, it might be useful at other places.
/// (iw |)
pub fn opCall() callconv(.Naked) noreturn {
asm volatile (
\\ leaq 16(%%r12), %%rax
\\ subq $0x8, %%r13
\\ movq %%rax, (%%r13)
\\ movq 8(%%r12), %%r12
\\ jmpq *(%%r12)
);
}
/// (addr) /// (addr)
pub fn opReturn() callconv(.Naked) noreturn { pub fn opReturn() callconv(.Naked) noreturn {
asm volatile ( // https://www.felixcloutier.com/x86/ret
\\ movq (%%r13), %%r12 asm volatile ("ret");
\\ addq $0x08, %%r13
\\ jmpq *(%%r12)
);
} }
// todo: Make sure it's non reentry in one given thread.
pub fn execute(binary: []const Word, entry_addr: usize) void { pub fn execute(binary: []const Word, entry_addr: usize) void {
@setCold(true);
// todo: Ensure correctness. // todo: Ensure correctness.
// https://wiki.osdev.org/System_V_ABI // https://wiki.osdev.org/System_V_ABI
// https://www.felixcloutier.com/x86/call
// todo: Use remaining stack as return.
// Such device is used so that opReturn could be used for return.
asm volatile ( asm volatile (
\\ movq $0f, 8(%%r13) \\ call *(%%rdi)
\\ leaq 8(%%r13), %%rax
\\ movq %%rax, (%%r13)
\\ jmpq *(%%r12)
\\ 0:
: :
: [thread] "r" (&binary[entry_addr]), : [thread] "rdi" (&binary[entry_addr]),
[retstk] "r" (&return_stack[return_stack.len - 2]), : "rflags", "rax", "rbx", "rsp", "rbp", "r12", "r13", "r14", "r15", "rsi", "rdx", "rcx", "r8", "r9", "r10", "r11", "memory"
: "rflags", "rax", "rbx", "rsp", "rdi", "rbp", "r14", "r15", "rsi", "rdx", "rcx", "r8", "r9", "r10", "r11", "memory"
); );
} }

View File

@ -14,4 +14,4 @@
// idea: 'JIT' could be done by simple op* compiled binary copying up until `jmpq *(%%rdi)`, // idea: 'JIT' could be done by simple op* compiled binary copying up until `jmpq *(%%rdi)`,
// with immediate operand prelude modified, which could be done procedurally. // with immediate operand prelude modified, which could be done procedurally.
pub usingnamespace @import("arch/x86-64.zig"); usingnamespace @import("arch/x86-64.zig");

View File

@ -1,23 +1,16 @@
const int = @import("interpreter.zig"); const int = @import("interpreter.zig");
pub fn main() !void { pub fn main() !void {
// todo: Mixing return addresses in stack poses a challenge, hm. const binary = [_]int.Word{
const add = [_]int.Word{ @as(int.Word, @intFromPtr(&int.opPushWord)),
~@as(int.Word, 1),
@as(int.Word, @intFromPtr(&int.opPushWord)),
~@as(int.Word, 1),
@as(int.Word, @intFromPtr(&int.opSumWordsWithOverflow)), @as(int.Word, @intFromPtr(&int.opSumWordsWithOverflow)),
@as(int.Word, @intFromPtr(&int.opReturn)),
};
const entry = [_]int.Word{
@as(int.Word, @intFromPtr(&int.opPushWord)),
1,
@as(int.Word, @intFromPtr(&int.opPushWord)),
2,
@as(int.Word, @intFromPtr(&int.opCall)),
@as(int.Word, @intFromPtr(&add)),
@as(int.Word, @intFromPtr(&int.opSinkWord)), @as(int.Word, @intFromPtr(&int.opSinkWord)),
@as(int.Word, @intFromPtr(&int.opSinkWord)), @as(int.Word, @intFromPtr(&int.opSinkWord)),
@as(int.Word, @intFromPtr(&int.opReturn)), @as(int.Word, @intFromPtr(&int.opReturn)),
}; };
int.execute(&entry, 0); int.execute(&binary, 0);
} }