Compare commits

..

1 Commits

Author SHA1 Message Date
veclav talica
d5f3944b1a cosmopolitan build 2023-09-21 11:43:12 +05:00
8 changed files with 24 additions and 53 deletions

View File

@ -1,5 +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
zig build-obj ./src/main.zig -fno-PIE -fno-PIC -fno-stack-check
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

View File

@ -4,4 +4,4 @@ cd ./extern
./get.sh
cd ../
zig translate-c ./extern/cosmopolitan/cosmopolitan.h > ./src/platform/cosmopolitan/cosmopolitan.zig
zig translate-c ./extern/cosmopolitan/cosmopolitan.h > ./src/cosmopolitan/cosmopolitan.zig

View File

@ -1,2 +1,2 @@
pub usingnamespace @import("x86-64/jedino-jedro.zig");
pub usingnamespace @import("x86-64/zov/ve-sistema.zig");
pub usingnamespace @import("x86-64/ve-sistema.zig");

View File

@ -14,9 +14,6 @@
// 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/
@ -26,25 +23,10 @@
// 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;
@ -53,7 +35,7 @@ const Word = tolmac.Word;
/// (iw | -- iw)
pub fn opPushWord() callconv(.Naked) noreturn {
asm volatile (
\\ addq $0x10, %%r12
\\ add $0x10, %%r12
\\ pushq -8(%%r12)
\\ jmpq *(%%r12)
);
@ -63,7 +45,7 @@ pub fn opPushWord() callconv(.Naked) noreturn {
/// (w)
pub fn opSinkWord() callconv(.Naked) noreturn {
asm volatile (
\\ addq $0x08, %%r12
\\ add $0x08, %%r12
\\ addq $0x08, %%rsp
\\ jmpq *(%%r12)
);
@ -83,29 +65,20 @@ pub fn opSinkWord() callconv(.Naked) noreturn {
// @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/add
// 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), %%rbx
\\ movq %%rbx, %%rax
\\ addq 8(%%rsp), %%rax
\\ movq (%%rsp), %%rax
\\ adcq 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)
\\ setc %%al
\\ movb %%al, (%%rsp)
\\ jmpq *(%%r12)
);
}
@ -119,6 +92,9 @@ pub fn opSumWords() callconv(.Naked) noreturn {
// @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 (

View File

@ -1,16 +1,12 @@
//! .zov.ve-sistema:x86-64
//! 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
// 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");
const tolmac = @import("../../tolmac.zig");
/// Used for stack parameter passing.
pub const WordLimit = 128;
@ -115,8 +111,8 @@ pub fn generateOpZovSysvFromPrototype(prototype: anytype) !*const fn () callconv
;
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;
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;

View File

View File

@ -1,5 +1,5 @@
const tolmac = @import("nmvm").tolmac;
const cosmo = @import("cosmopolitan.zig");
const tolmac = @import("tolmac.zig");
const cosmo = @import("cosmopolitan/cosmopolitan.zig");
fn printInt3(int: u64, other: u32, another: u16) callconv(.SysV) void {
@setAlignStack(16);
@ -20,7 +20,7 @@ comptime {
// todo: No cosmopolitan main.
fn cosmopolitanMain(argc: c_int, argv: [*][*:0]u8) callconv(.SysV) c_int {
export fn cosmopolitanMain(argc: c_int, argv: [*][*:0]u8) c_int {
_ = argc;
_ = argv;
@ -57,5 +57,5 @@ fn cosmopolitanMain(argc: c_int, argv: [*][*:0]u8) callconv(.SysV) c_int {
}
test {
_ = @import("nmvm").tolmac;
_ = @import("tolmac.zig");
}

View File

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