Compare commits
No commits in common. "a6fd0c802359b08a93ff6804265ec4a84645909f" and "b66c3dca14e3bf84c366034d707c2cee28b38e25" have entirely different histories.
a6fd0c8023
...
b66c3dca14
@ -131,13 +131,10 @@ pub fn execute(binary: []const Word, entry_addr: usize) void {
|
||||
|
||||
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 %%rsp, %%rbp
|
||||
\\
|
||||
\\ movq %%rdi, %%r12
|
||||
\\ movq %%rsi, %%r13
|
||||
@ -149,8 +146,6 @@ fn jumpstartNakedSysV() callconv(.Naked) void {
|
||||
\\ jmpq *(%%r12)
|
||||
\\ 0:
|
||||
\\
|
||||
\\ popq %%r12
|
||||
\\ popq %%r13
|
||||
\\ popq %%rbp
|
||||
\\ ret
|
||||
);
|
||||
|
@ -6,7 +6,6 @@
|
||||
// https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf
|
||||
|
||||
const std = @import("std");
|
||||
const tolmac = @import("../../tolmac.zig");
|
||||
|
||||
/// Used for stack parameter passing.
|
||||
pub const WordLimit = 128;
|
||||
@ -32,7 +31,7 @@ fn determiteClass(comptime T: type, buffer: []Class) []Class {
|
||||
0 => buffer[0] = .void,
|
||||
1...64 => buffer[0] = .integer,
|
||||
65...128 => @compileError("unimplemented"),
|
||||
else => @compileError("invalid sysv parameter"),
|
||||
else => @compileError("unimplemented"),
|
||||
}
|
||||
},
|
||||
.Float => |float| {
|
||||
@ -41,13 +40,16 @@ fn determiteClass(comptime T: type, buffer: []Class) []Class {
|
||||
1...64 => buffer[0] = .sse,
|
||||
65...80 => @compileError("unimplemented"),
|
||||
81...128 => @compileError("unimplemented"),
|
||||
else => @compileError("invalid sysv parameter"),
|
||||
else => @compileError("unimplemented"),
|
||||
}
|
||||
},
|
||||
.Bool => buffer[0] = .integer,
|
||||
.Pointer => |ptr| {
|
||||
switch (ptr.size) {
|
||||
.Slice => @compileError("invalid sysv parameter"),
|
||||
.Slice => {
|
||||
buffer[0] = .integer;
|
||||
buffer[1] = .integer;
|
||||
},
|
||||
else => buffer[0] = .integer,
|
||||
}
|
||||
},
|
||||
@ -67,34 +69,24 @@ pub fn generateOpZovSysvFromPrototype(prototype: anytype) !*const fn () callconv
|
||||
// > 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;
|
||||
|
||||
// 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.
|
||||
|
||||
// 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
|
||||
\\ subq $0x8, %%rsp
|
||||
\\
|
||||
;
|
||||
|
||||
@ -110,6 +102,9 @@ pub fn generateOpZovSysvFromPrototype(prototype: anytype) !*const fn () callconv
|
||||
\\
|
||||
;
|
||||
|
||||
@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;
|
||||
@ -123,12 +118,6 @@ pub fn generateOpZovSysvFromPrototype(prototype: anytype) !*const fn () callconv
|
||||
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| {
|
||||
@ -171,84 +160,3 @@ pub fn generateOpZovSysvFromPrototype(prototype: anytype) !*const fn () callconv
|
||||
}.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");
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
test "integer register 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.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);
|
||||
}
|
||||
|
21
src/main.zig
21
src/main.zig
@ -1,20 +1,15 @@
|
||||
const std = @import("std");
|
||||
const tolmac = @import("tolmac.zig");
|
||||
|
||||
fn printInt3(int: u64, other: u32, another: u16) callconv(.SysV) void {
|
||||
fn printInt(int: u64, other: u32, another: u16) callconv(.SysV) void {
|
||||
@setAlignStack(16);
|
||||
std.debug.print("test: {}, {}, {}\n", .{ int, other, another });
|
||||
}
|
||||
|
||||
fn printInt2(int: u64, other: u8) callconv(.SysV) void {
|
||||
@setAlignStack(16);
|
||||
std.debug.print("test: {}, {}\n", .{ int, other });
|
||||
}
|
||||
|
||||
const opPrintInt3Zov = tolmac.generateOpZovSysvFromPrototype(printInt3) catch unreachable;
|
||||
const opPrintInt2Zov = tolmac.generateOpZovSysvFromPrototype(printInt2) catch unreachable;
|
||||
const opPrintIntZov = tolmac.generateOpZovSysvFromPrototype(printInt) catch unreachable;
|
||||
|
||||
pub fn main() !void {
|
||||
// todo: Mixing return addresses in stack poses a challenge, hm.
|
||||
const add = [_]tolmac.Word{
|
||||
@as(tolmac.Word, @intFromPtr(&tolmac.opSumWordsWithOverflow)),
|
||||
@as(tolmac.Word, @intFromPtr(&tolmac.opReturn)),
|
||||
@ -32,11 +27,9 @@ pub fn main() !void {
|
||||
10,
|
||||
@as(tolmac.Word, @intFromPtr(&tolmac.opPushWord)),
|
||||
20,
|
||||
@as(tolmac.Word, @intFromPtr(opPrintInt3Zov)),
|
||||
@as(tolmac.Word, @intFromPtr(&printInt3)),
|
||||
@as(tolmac.Word, @intFromPtr(opPrintIntZov)),
|
||||
@as(tolmac.Word, @intFromPtr(&printInt)),
|
||||
@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)),
|
||||
@ -44,7 +37,3 @@ pub fn main() !void {
|
||||
|
||||
tolmac.execute(&entry, 0);
|
||||
}
|
||||
|
||||
test {
|
||||
_ = @import("tolmac.zig");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user