Compare commits

...

3 Commits

Author SHA1 Message Date
veclav talica
b66c3dca14 sysv zov generation for low overhead ffi 2023-09-19 22:54:27 +05:00
veclav talica
0d5bd46412 return_stack placed on stack, this allows for reentry and no TLS switch for threads; makes more sense 2023-09-19 11:57:53 +05:00
veclav talica
d97dcff494 restructuring 2023-09-19 11:55:20 +05:00
6 changed files with 346 additions and 157 deletions

2
gdb.sh
View File

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

View File

@ -1,126 +1,2 @@
// 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:
// r12 <- binary thread pointer
// r13 <- return stack pointer
// 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;
pub const RecursionLimit = 1024;
threadlocal var return_stack: [RecursionLimit + 1]Word = undefined;
// todo: Variant that pushes array of words.
/// (iw | -- iw)
pub fn opPushWord() callconv(.Naked) noreturn {
asm volatile (
\\ add $0x10, %%r12
\\ pushq -8(%%r12)
\\ jmpq *(%%r12)
);
}
// todo: Variant that discards array of words.
/// (w --)
pub fn opSinkWord() callconv(.Naked) noreturn {
asm volatile (
\\ add $0x08, %%r12
\\ addq $0x08, %%rsp
\\ jmpq *(%%r12)
);
}
/// (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 (
\\ addq $0x08, %%r12
\\ movq (%%rsp), %%rax
\\ adcq 8(%%rsp), %%rax
\\ movq %%rax, 8(%%rsp)
\\ setc %%al
\\ movb %%al, (%%rsp)
\\ jmpq *(%%r12)
);
}
// 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 });
// }
// 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)
pub fn opReturn() callconv(.Naked) noreturn {
asm volatile (
\\ movq (%%r13), %%r12
\\ 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 {
@setCold(true);
// todo: Ensure correctness.
// https://wiki.osdev.org/System_V_ABI
// todo: Use remaining stack as return.
// Such device is used so that opReturn could be used for return.
asm volatile (
\\ movq $0f, 8(%%r13)
\\ leaq 8(%%r13), %%rax
\\ movq %%rax, (%%r13)
\\ jmpq *(%%r12)
\\ 0:
:
: [thread] "r" (&binary[entry_addr]),
[retstk] "r" (&return_stack[return_stack.len - 2]),
: "rflags", "rax", "rbx", "rsp", "rdi", "rbp", "r14", "r15", "rsi", "rdx", "rcx", "r8", "r9", "r10", "r11", "memory"
);
}
pub usingnamespace @import("x86-64/jedino-jedro.zig");
pub usingnamespace @import("x86-64/ve-sistema.zig");

View File

@ -0,0 +1,152 @@
//! jedino jedro (.jj:x86-64)
//!
//! Desired properties:
//! - OS agnosticism, meaning it tries to respect conventions posed by target OSes.
//! For extensions based open it there should be an enum value indicating host,
//! for example, when dealing with extern C functions of shared objects.
//! Stack/thread pointers are chosen so that SysV and MS abis callee side preserve those,
//! so that we don't need to constantly push and restore on procedure call.
// Execution thread convention:
// r12 <- binary thread pointer
// r13 <- return stack pointer
// r14 <- extension context pointer
// todo: Use ZF flag as conditional register so to not involve stack?
// Alternatively we could keep boolean word, but implement it in vector semantics.
// Resources:
// 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
// https://stackoverflow.com/questions/37639993/is-this-assembly-function-call-safe-complete
// Neat things:
// https://joryanick.com/retro-fast-x86-memcpy.php
// https://www.codeproject.com/Articles/1110153/Apex-memmove-the-fastest-memcpy-memmove-on-x-x-EVE
const tolmac = @import("../../tolmac.zig");
const Word = tolmac.Word;
// todo: Variant that pushes array of words.
/// (iw | -- iw)
pub fn opPushWord() callconv(.Naked) noreturn {
asm volatile (
\\ add $0x10, %%r12
\\ pushq -8(%%r12)
\\ jmpq *(%%r12)
);
}
// todo: Variant that discards array of words.
/// (w)
pub fn opSinkWord() callconv(.Naked) noreturn {
asm volatile (
\\ add $0x08, %%r12
\\ addq $0x08, %%rsp
\\ jmpq *(%%r12)
);
}
/// (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 (
\\ addq $0x08, %%r12
\\ movq (%%rsp), %%rax
\\ adcq 8(%%rsp), %%rax
\\ movq %%rax, 8(%%rsp)
\\ setc %%al
\\ movb %%al, (%%rsp)
\\ jmpq *(%%r12)
);
}
// 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 });
// }
// 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)
pub fn opReturn() callconv(.Naked) noreturn {
asm volatile (
\\ movq (%%r13), %%r12
\\ addq $0x08, %%r13
\\ jmpq *(%%r12)
);
}
// todo: Allow passing initial stack via array of words.
// todo: Ensure correctness.
// todo: Use remaining stack as return.
// todo: Make it .C callconv and extern.
// todo: Permute by calling conventions.
pub fn execute(binary: []const Word, entry_addr: usize) void {
// https://wiki.osdev.org/System_V_ABI
@setCold(true);
var return_stack: [tolmac.RecursionLimit + 1]Word = undefined;
jumpstartSysV(&binary[entry_addr], &return_stack[return_stack.len - 2]);
}
const jumpstartSysV = @as(*const fn (thread: *const Word, return_stack: *Word) callconv(.SysV) void, @ptrCast(&jumpstartNakedSysV));
fn jumpstartNakedSysV() callconv(.Naked) void {
asm volatile (
\\ pushq %%rbp
\\ movq %%rsp, %%rbp
\\
\\ movq %%rdi, %%r12
\\ movq %%rsi, %%r13
\\
\\ # Such device is used so that opReturn could be used for return.
\\ movq $0f, 8(%%r13)
\\ leaq 8(%%r13), %%rax
\\ movq %%rax, (%%r13)
\\ jmpq *(%%r12)
\\ 0:
\\
\\ popq %%rbp
\\ ret
);
}

View File

@ -0,0 +1,162 @@
//! ve sistema (.ve-sistema:x86-64)
//!
//! Provides entry opcodes for System V calling convention, optimized for specific prototypes.
//!
// https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf
const std = @import("std");
/// Used for stack parameter passing.
pub const WordLimit = 128;
const AsmBufferLimit = 4096;
const ClassBufferLimit = 256;
const Class = enum {
void, // Denotes empty types.
integer,
sse,
sseup,
x87,
x87up,
no_class,
memory,
};
fn determiteClass(comptime T: type, buffer: []Class) []Class {
switch (@typeInfo(T)) {
.Void => &[1]Class{.void},
.Int => |int| {
switch (int.bits) {
0 => buffer[0] = .void,
1...64 => buffer[0] = .integer,
65...128 => @compileError("unimplemented"),
else => @compileError("unimplemented"),
}
},
.Float => |float| {
switch (float.bits) {
0 => buffer[0] = .void,
1...64 => buffer[0] = .sse,
65...80 => @compileError("unimplemented"),
81...128 => @compileError("unimplemented"),
else => @compileError("unimplemented"),
}
},
.Bool => buffer[0] = .integer,
.Pointer => |ptr| {
switch (ptr.size) {
.Slice => {
buffer[0] = .integer;
buffer[1] = .integer;
},
else => buffer[0] = .integer,
}
},
.Fn => buffer[0] = .integer,
else => @compileError("unimplemented"),
}
return buffer[0 .. (@sizeOf(T) - 1) / 8 + 1];
}
// todo: Make sure duplicates are not made.
// todo: Cache results for identical in effect devices.
//
/// (iw | -- (arbitrary amount of words))
pub fn generateOpZovSysvFromPrototype(prototype: anytype) !*const fn () callconv(.Naked) noreturn {
// todo: Should we care about this?
// > The direction flag DF in the %rFLAGS register must be clear (set to forward
// > direction) on function entry and return.
comptime {
const func = @typeInfo(@TypeOf(prototype)).Fn;
var source_buffer = [_]u8{0} ** AsmBufferLimit;
var source_needle: usize = 0;
// todo: Align callee frame to 16?
// > shrq $4, %%rsp
// > addq $1, %%rsp
// > shlq $4, %%rsp
// idea: Try using REP for big consequent memory pushes.
// todo: In-stack returns by pointing %rdi directly to final destination.
const Prelude =
\\ movq %%rsp, %%rbp # Move stack pointer in non-volatile %rbp to restore later
\\ subq $0x8, %%rsp
\\
;
const Call =
\\ call *8(%%r12)
\\
;
const Epilogue =
\\ movq %%rbp, %%rsp # Restore stack pointer
\\ addq $0x10, %%r12
\\ jmpq *(%%r12)
\\
;
@memcpy(source_buffer[source_needle .. source_needle + Prelude.len], Prelude[0..]);
source_needle += Prelude.len;
var integer_allocation: usize = 0;
const IntegerAllocations = [_][]const u8{ "rdi", "rsi", "rdx", "rcx", "r8", "r9", "stack" };
// var sse_allocation: enum { xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, stack } = .xmm0;
var class_buffer = [_]Class{.void} ** ClassBufferLimit;
// Calculate stack space used by parameters.
var parameter_stack_size: usize = 0;
for (func.params) |param| {
const classes = determiteClass(param.type.?, &class_buffer);
parameter_stack_size += 8 * classes.len;
}
// Push parameters to appropriate registers and stack positions.
var stack_offset: usize = parameter_stack_size;
for (func.params) |param| {
const classes = determiteClass(param.type.?, &class_buffer);
for (classes) |class| {
stack_offset -= 8;
switch (class) {
.integer => {
if (integer_allocation < IntegerAllocations.len - 1) {
source_needle += (try std.fmt.bufPrint(
source_buffer[source_needle..],
"movq {}(%%rbp), %%{s}\n",
.{ stack_offset, IntegerAllocations[integer_allocation] },
)).len;
integer_allocation += 1;
} else {
source_needle += (try std.fmt.bufPrint(
source_buffer[source_needle..],
"pushq {}(%%rbp)\n",
.{stack_offset},
)).len;
}
},
.void => {},
else => @compileError("unimplemented"),
}
}
}
@memcpy(source_buffer[source_needle .. source_needle + Call.len], Call[0..]);
source_needle += Call.len;
@memcpy(source_buffer[source_needle .. source_needle + Epilogue.len], Epilogue[0..]);
source_needle += Epilogue.len;
return &struct {
fn op() callconv(.Naked) noreturn {
asm volatile (source_buffer[0..source_needle]);
}
}.op;
}
}

View File

@ -1,17 +0,0 @@
// 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.
pub usingnamespace @import("arch/x86-64.zig");

View File

@ -1,23 +1,39 @@
const int = @import("interpreter.zig");
const std = @import("std");
const tolmac = @import("tolmac.zig");
fn printInt(int: u64, other: u32, another: u16) callconv(.SysV) void {
@setAlignStack(16);
std.debug.print("test: {}, {}, {}\n", .{ int, other, another });
}
const opPrintIntZov = tolmac.generateOpZovSysvFromPrototype(printInt) catch unreachable;
pub fn main() !void {
// todo: Mixing return addresses in stack poses a challenge, hm.
const add = [_]int.Word{
@as(int.Word, @intFromPtr(&int.opSumWordsWithOverflow)),
@as(int.Word, @intFromPtr(&int.opReturn)),
const add = [_]tolmac.Word{
@as(tolmac.Word, @intFromPtr(&tolmac.opSumWordsWithOverflow)),
@as(tolmac.Word, @intFromPtr(&tolmac.opReturn)),
};
const entry = [_]int.Word{
@as(int.Word, @intFromPtr(&int.opPushWord)),
const entry = [_]tolmac.Word{
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
1,
@as(int.Word, @intFromPtr(&int.opPushWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.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.opReturn)),
@as(tolmac.Word, @intFromPtr(&tolmac.opCall)),
@as(tolmac.Word, @intFromPtr(&add)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
10,
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
20,
@as(tolmac.Word, @intFromPtr(opPrintIntZov)),
@as(tolmac.Word, @intFromPtr(&printInt)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opReturn)),
};
int.execute(&entry, 0);
tolmac.execute(&entry, 0);
}