Compare commits

..

10 Commits

14 changed files with 557 additions and 150 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
**/*.o
**/*.a
**/*.lds
**/*.h
zig-out/
zig-cache/

0
.gitmodules vendored Normal file
View File

5
build-cosmo.sh Executable file
View File

@ -0,0 +1,5 @@
#!/bin/sh
zig build-obj ./src/platform/cosmopolitan/main.zig -fno-PIE -fno-PIC -fno-stack-check --mod nmvm::src/nmvm.zig --deps nmvm
ld.bfd main.o -o zig-out/bin/nmvm.com.dbg -T extern/cosmopolitan/ape.lds extern/cosmopolitan/crt.o extern/cosmopolitan/ape-copy-self.o \
extern/cosmopolitan/cosmopolitan.a -z common-page-size=0x1000 -z max-page-size=0x1000 --gc-sections

4
extern/cosmopolitan/get.sh vendored Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
wget wget https://justine.lol/cosmopolitan/cosmopolitan.tar.gz
unzip cosmopolitan.tar.gz

4
extern/get.sh vendored Executable file
View File

@ -0,0 +1,4 @@
#!/bin/sh
cd ./cosmopolitan
./get.sh

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'

7
prepare.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/sh
cd ./extern
./get.sh
cd ../
zig translate-c ./extern/cosmopolitan/cosmopolitan.h > ./src/platform/cosmopolitan/cosmopolitan.zig

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/zov/ve-sistema.zig");

View File

@ -0,0 +1,181 @@
//! 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.
//
// ZF flag route can be achieved with using LEA instead of ADD as it doesn't touch flags,
// but we would need to store the flag when doing other calling convention calls.
// 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
// https://groups.csail.mit.edu/pag/OLD/parg/piumarta98optimizing.pdf
// https://dl.acm.org/doi/pdf/10.1145/1328195.1328197
// https://www.agner.org/optimize/instruction_tables.pdf
// https://stackoverflow.com/questions/6323027/lea-or-add-instruction
// 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
// https://www.usenix.org/legacy/publications/library/proceedings/jvm01/gagnon/gagnon_html/node19.html#piumarta
// https://www.agner.org/optimize/optimizing_assembly.pdf
// http://sebastianmihai.com/x86-assembly-optimization.html
// todo: Opcode for fast integer divisions with statically known divider.
// todo: Opcodes taking operands based on offset from top of stack, without consuming.
// todo: Certain constant multiplication optimization by LEA:
// LEA EAX, [EAX * 2 + EAX] ;EAX = EAX * 3
// LEA EAX, [EAX * 4 + EAX] ;EAX = EAX * 5
// LEA EAX, [EAX * 8 + EAX] ;EAX = EAX * 9
//
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 (
\\ addq $0x10, %%r12
\\ pushq -8(%%r12)
\\ jmpq *(%%r12)
);
}
// todo: Variant that discards array of words.
/// (w)
pub fn opSinkWord() callconv(.Naked) noreturn {
asm volatile (
\\ addq $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 });
// }
/// (w1 w2 -- sum overflow)
pub fn opSumWordsWithOverflow() callconv(.Naked) noreturn {
// https://www.felixcloutier.com/x86/add
// https://www.felixcloutier.com/x86/setcc
asm volatile (
\\ addq $0x08, %%r12
\\ movq (%%rsp), %%rbx
\\ movq %%rbx, %%rax
\\ addq 8(%%rsp), %%rax
\\ movq %%rax, 8(%%rsp)
\\ xorq %%rbx, (%%rsp)
\\ setc (%%rsp)
\\ jmpq *(%%r12)
);
}
/// (w1 w2 -- sum)
pub fn opSumWords() callconv(.Naked) noreturn {
asm volatile (
\\ addq $0x08, %%r12
\\ popq %%rax
\\ addq (%%rsp), %%rax
\\ movq %%rax, (%%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 });
// }
/// (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));
// todo: Make sure to save every non-volatile register and restore it.
// todo: Should we make frames well formed for walking?
fn jumpstartNakedSysV() callconv(.Naked) void {
asm volatile (
\\ pushq %%rbp
\\ pushq %%r12
\\ pushq %%r13
\\
\\ 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 %%r12
\\ popq %%r13
\\ popq %%rbp
\\ ret
);
}

View File

@ -0,0 +1,282 @@
//! .zov.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
// todo: Provide opcode that would dynamically dispatch based on marshaled C prototypes,
// which will be sufficient for rare prototypes sporadically used, so to not bloat
// the binary with all possible permutations or ask for them on comptime, which is unreasonable.
const std = @import("std");
const tolmac = @import("../../../tolmac.zig");
/// 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("invalid sysv parameter"),
}
},
.Float => |float| {
switch (float.bits) {
0 => buffer[0] = .void,
1...64 => buffer[0] = .sse,
65...80 => @compileError("unimplemented"),
81...128 => @compileError("unimplemented"),
else => @compileError("invalid sysv parameter"),
}
},
.Bool => buffer[0] = .integer,
.Pointer => |ptr| {
switch (ptr.size) {
.Slice => @compileError("invalid sysv parameter"),
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.
// todo: Is our bool convention compatible?
// > Booleans, when stored in a memory object, are stored as single byte objects the
// > value of which is always 0 (false) or 1 (true). When stored in integer registers
// > (except for passing as arguments), all 8 bytes of the register are significant; any
// > nonzero value is considered true.
comptime {
const func = @typeInfo(@TypeOf(prototype)).Fn;
if (func.calling_convention != .SysV)
@compileError("Non SysV function passed");
var source_buffer = [_]u8{0} ** AsmBufferLimit;
var source_needle: usize = 0;
// idea: Try using REP for big consequent memory pushes.
// todo: In-stack returns by pointing %rdi directly to final destination.
// todo: Handle cases with more than 32 eightbytes on stack.
// For that we would need to increment %rbp by 256 every once in a while,
// either by storing its copy in volatile register or subtracting back after copy it done.
// todo: Test whether aligning by shifting is better.
const Prelude =
\\ movq %%rsp, %%rbp # Move stack pointer in non-volatile %rbp to restore later
\\ andq $-16, %%rsp # Align stack so that %rsp + 8 in callee is 16 aligned.
\\ subq $0x{x}, %%rsp
\\
;
const Call =
\\ call *8(%%r12)
\\
;
const Epilogue =
\\ movq %%rbp, %%rsp # Restore stack pointer
\\ addq $0x10, %%r12
\\ jmpq *(%%r12)
\\
;
var integer_allocation: usize = 0;
const IntegerAllocations = [_][]const u8{ "rdi", "rsi", "rdx", "rcx", "r8", "r9" };
// var sse_allocation: enum { xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7 } = .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;
}
source_needle += (try std.fmt.bufPrint(
source_buffer[source_needle..],
Prelude[0..],
.{if (parameter_stack_size % 16 == 0) 16 else 8},
)).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;
}
}
fn addInt1(a: u64) callconv(.SysV) void {
@setAlignStack(16);
if (a != 1)
@panic("addInt1");
}
fn addInt2(a: u64, b: u8) callconv(.SysV) void {
@setAlignStack(16);
if ((a + b) != 3)
@panic("addInt2");
}
fn addInt3(a: u64, b: u32, c: u16) callconv(.SysV) void {
@setAlignStack(16);
if ((a + b + c) != 6)
@panic("addInt3");
}
fn addInt4(a: u64, b: u32, c: u16, d: u8) callconv(.SysV) void {
@setAlignStack(16);
if ((a + b + c + d) != 10)
@panic("addInt4");
}
fn addInt5(a: u64, b: u32, c: u16, d: u8, e: u64) callconv(.SysV) void {
@setAlignStack(16);
if ((a + b + c + d + e) != 11)
@panic("addInt5");
}
fn addInt6(a: u64, b: u32, c: u16, d: u8, e: u64, f: i32) callconv(.SysV) void {
@setAlignStack(16);
if ((a + b + c + d + e + @as(u64, @intCast(f))) != 15)
@panic("addInt6");
}
fn addInt7(a: u64, b: u32, c: u16, d: u8, e: u64, f: i32, sa: u32) callconv(.SysV) void {
@setAlignStack(16);
if ((a + b + c + d + e + @as(u64, @intCast(f)) + sa) != 30)
@panic("addInt7");
}
fn addInt8(a: u64, b: u32, c: u16, d: u8, e: u64, f: i32, sa: u32, sb: u8) callconv(.SysV) void {
@setAlignStack(16);
if ((a + b + c + d + e + @as(u64, @intCast(f)) + sa + sb) != 40)
@panic("addInt8");
}
const opZov1Int = generateOpZovSysvFromPrototype(addInt1) catch unreachable;
const opZov2Ints = generateOpZovSysvFromPrototype(addInt2) catch unreachable;
const opZov3Ints = generateOpZovSysvFromPrototype(addInt3) catch unreachable;
const opZov4Ints = generateOpZovSysvFromPrototype(addInt4) catch unreachable;
const opZov5Ints = generateOpZovSysvFromPrototype(addInt5) catch unreachable;
const opZov6Ints = generateOpZovSysvFromPrototype(addInt6) catch unreachable;
const opZov7Ints = generateOpZovSysvFromPrototype(addInt7) catch unreachable;
const opZov8Ints = generateOpZovSysvFromPrototype(addInt8) catch unreachable;
test "integer parameter passing" {
const code = [_]tolmac.Word{
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
1,
@as(tolmac.Word, @intFromPtr(opZov1Int)),
@as(tolmac.Word, @intFromPtr(&addInt1)),
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
2,
@as(tolmac.Word, @intFromPtr(opZov2Ints)),
@as(tolmac.Word, @intFromPtr(&addInt2)),
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
3,
@as(tolmac.Word, @intFromPtr(opZov3Ints)),
@as(tolmac.Word, @intFromPtr(&addInt3)),
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
4,
@as(tolmac.Word, @intFromPtr(opZov4Ints)),
@as(tolmac.Word, @intFromPtr(&addInt4)),
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
1,
@as(tolmac.Word, @intFromPtr(opZov5Ints)),
@as(tolmac.Word, @intFromPtr(&addInt5)),
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
4,
@as(tolmac.Word, @intFromPtr(opZov6Ints)),
@as(tolmac.Word, @intFromPtr(&addInt6)),
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
15,
@as(tolmac.Word, @intFromPtr(opZov7Ints)),
@as(tolmac.Word, @intFromPtr(&addInt7)),
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
10,
@as(tolmac.Word, @intFromPtr(opZov8Ints)),
@as(tolmac.Word, @intFromPtr(&addInt8)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opReturn)),
};
tolmac.execute(&code, 0);
}

View File

@ -1,23 +0,0 @@
const int = @import("interpreter.zig");
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 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.opReturn)),
};
int.execute(&entry, 0);
}

1
src/nmvm.zig Normal file
View File

@ -0,0 +1 @@
pub const tolmac = @import("tolmac.zig");

View File

@ -0,0 +1,61 @@
const tolmac = @import("nmvm").tolmac;
const cosmo = @import("cosmopolitan.zig");
fn printInt3(int: u64, other: u32, another: u16) callconv(.SysV) void {
@setAlignStack(16);
_ = cosmo.printf("%u\n", @as(c_uint, @intCast(int + other + another)));
}
fn printInt2(int: u64, other: u8) callconv(.SysV) void {
@setAlignStack(16);
_ = cosmo.printf("%u\n", @as(c_uint, @intCast(int + other)));
}
const opPrintInt3Zov = tolmac.generateOpZovSysvFromPrototype(printInt3) catch unreachable;
const opPrintInt2Zov = tolmac.generateOpZovSysvFromPrototype(printInt2) catch unreachable;
comptime {
@export(cosmopolitanMain, .{ .name = "main" });
}
// todo: No cosmopolitan main.
fn cosmopolitanMain(argc: c_int, argv: [*][*:0]u8) callconv(.SysV) c_int {
_ = argc;
_ = argv;
const add = [_]tolmac.Word{
@as(tolmac.Word, @intFromPtr(&tolmac.opSumWordsWithOverflow)),
@as(tolmac.Word, @intFromPtr(&tolmac.opReturn)),
};
const entry = [_]tolmac.Word{
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
1,
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
2,
@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(opPrintInt3Zov)),
@as(tolmac.Word, @intFromPtr(&printInt3)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(opPrintInt2Zov)),
@as(tolmac.Word, @intFromPtr(&printInt2)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opSinkWord)),
@as(tolmac.Word, @intFromPtr(&tolmac.opReturn)),
};
tolmac.execute(&entry, 0);
return 0;
}
test {
_ = @import("nmvm").tolmac;
}

View File

@ -14,4 +14,7 @@
// 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 const Word = u64;
pub const RecursionLimit = 1024;
pub usingnamespace @import("arch/x86-64.zig");