start of x86-64 implementation; stack pushing, sinking, add with overflow, procedure return ops
This commit is contained in:
91
src/arch/x86-64.zig
Normal file
91
src/arch/x86-64.zig
Normal file
@ -0,0 +1,91 @@
|
||||
// Execution thread convention:
|
||||
// rdi <- binary thread
|
||||
|
||||
// Resources used:
|
||||
// https://mort.coffee/home/fast-interpreters/
|
||||
// https://blog.reverberate.org/2021/04/21/musttail-efficient-interpreters.html
|
||||
// https://en.wikibooks.org/wiki/X86_Assembly/GNU_assembly_syntax
|
||||
// https://www.cs.princeton.edu/courses/archive/spr18/cos217/lectures/15_AssemblyFunctions.pdf
|
||||
// https://ziglang.org/documentation/master/#toc-Assembly
|
||||
// https://csiflabs.cs.ucdavis.edu/~ssdavis/50/att-syntax.htm
|
||||
|
||||
pub const Word = u64;
|
||||
|
||||
// todo: Variant that pushes array of words.
|
||||
/// (iw | -- iw)
|
||||
pub fn opPushWord() callconv(.Naked) noreturn {
|
||||
asm volatile (
|
||||
\\ add $0x10, %%rdi
|
||||
\\ pushq -8(%%rdi)
|
||||
\\ jmpq *(%%rdi)
|
||||
);
|
||||
}
|
||||
|
||||
// todo: Variant that discards array of words.
|
||||
/// (w --)
|
||||
pub fn opSinkWord() callconv(.Naked) noreturn {
|
||||
asm volatile (
|
||||
\\ add $0x08, %%rdi
|
||||
\\ addq $0x08, %%rsp
|
||||
\\ jmpq *(%%rdi)
|
||||
);
|
||||
}
|
||||
|
||||
/// (iw | -- (iw'nth word from stack) )
|
||||
// fn opTakeWord(binary: [*]const Word, cond: bool) noreturn {
|
||||
// @setRuntimeSafety(false);
|
||||
// takeWord(binary[1].word);
|
||||
// @call(.always_tail, binary[2].function, .{ &binary[2], cond });
|
||||
// }
|
||||
|
||||
/// (iw | w)
|
||||
// fn opSetWord(binary: [*]const Word, cond: bool) noreturn {
|
||||
// @setRuntimeSafety(false);
|
||||
// setWord(binary[1].word, popWord());
|
||||
// @call(.always_tail, binary[2].function, .{ &binary[2], cond });
|
||||
// }
|
||||
|
||||
// todo: Generate operation permutations procedurally.
|
||||
// todo: Jump on overflow instead of cond setting?
|
||||
/// (w1 w2 -- sum overflow)
|
||||
pub fn opSumWordsWithOverflow() callconv(.Naked) noreturn {
|
||||
// https://www.felixcloutier.com/x86/adc
|
||||
// https://www.felixcloutier.com/x86/setcc
|
||||
// idea: Could https://www.felixcloutier.com/x86/cmovcc be better for overflow push?
|
||||
asm volatile (
|
||||
\\ movq (%%rsp), %%rax
|
||||
\\ adcq 8(%%rsp), %%rax
|
||||
\\ movq %%rax, 8(%%rsp)
|
||||
\\ setc %%al
|
||||
\\ movb %%al, 7(%%rsp)
|
||||
\\ addq $0x08, %%rdi
|
||||
\\ jmpq *(%%rdi)
|
||||
);
|
||||
}
|
||||
|
||||
// todo: Generate operation permutations procedurally.
|
||||
// todo: We might not need cond register if conditions and jumps are combined?
|
||||
/// (w1 w2)
|
||||
// fn opRelativeJumpIfGreaterThan(binary: [*]const Word, cond: bool) noreturn {
|
||||
// @setRuntimeSafety(false);
|
||||
// const offset = if (popWord() > popWord()) binary[1].word else 2;
|
||||
// @call(.always_tail, binary[offset].function, .{ &binary[offset], cond });
|
||||
// }
|
||||
|
||||
/// (addr)
|
||||
pub fn opReturn() callconv(.Naked) noreturn {
|
||||
// https://www.felixcloutier.com/x86/ret
|
||||
asm volatile ("ret");
|
||||
}
|
||||
|
||||
pub fn execute(binary: []const Word, entry_addr: usize) void {
|
||||
// todo: Ensure correctness.
|
||||
// https://wiki.osdev.org/System_V_ABI
|
||||
// https://www.felixcloutier.com/x86/call
|
||||
asm volatile (
|
||||
\\ call *(%%rdi)
|
||||
:
|
||||
: [thread] "rdi" (&binary[entry_addr]),
|
||||
: "rflags", "rax", "rbx", "rsp", "rbp", "r12", "r13", "r14", "r15", "rsi", "rdx", "rcx", "r8", "r9", "r10", "r11", "memory"
|
||||
);
|
||||
}
|
17
src/interpreter.zig
Normal file
17
src/interpreter.zig
Normal file
@ -0,0 +1,17 @@
|
||||
// todo: Interpreter context as binary local variable.
|
||||
// It would hold memory mappings, as well as error stack.
|
||||
// todo: Define procedure call for user code.
|
||||
// todo: Instruction set extensions, such as memory management schemes, non-exhaustive logging,
|
||||
// exception mechanism, coroutines via yield/resume and etc.
|
||||
// todo: Threading scheme.
|
||||
// todo: Extension for native floating point stack ops.
|
||||
// todo: Try using small code model with nopie/nopic binary.
|
||||
|
||||
// idea: Specialized opcodes that have side effects on read and write, such as
|
||||
// zero-check on push/pop, or jump if condition bit met. This would create a lot
|
||||
// of permutations tho, we might try to discover which code devices are most used.
|
||||
|
||||
// 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.
|
||||
|
||||
usingnamespace @import("arch/x86-64.zig");
|
16
src/main.zig
Normal file
16
src/main.zig
Normal file
@ -0,0 +1,16 @@
|
||||
const int = @import("interpreter.zig");
|
||||
|
||||
pub fn main() !void {
|
||||
const binary = [_]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.opSinkWord)),
|
||||
@as(int.Word, @intFromPtr(&int.opSinkWord)),
|
||||
@as(int.Word, @intFromPtr(&int.opReturn)),
|
||||
};
|
||||
|
||||
int.execute(&binary, 0);
|
||||
}
|
Reference in New Issue
Block a user