Compare commits

..

2 Commits

Author SHA1 Message Date
veclav talica
a6fd0c8023 fix of stack in sysv call, test coverage for integers in registers 2023-09-20 12:26:42 +05:00
veclav talica
8de4140b00 stack alinment in sysv calls 2023-09-20 11:15:35 +05:00
3 changed files with 129 additions and 21 deletions

View File

@ -131,10 +131,13 @@ 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)); 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 { fn jumpstartNakedSysV() callconv(.Naked) void {
asm volatile ( asm volatile (
\\ pushq %%rbp \\ pushq %%rbp
\\ movq %%rsp, %%rbp \\ pushq %%r12
\\ pushq %%r13
\\ \\
\\ movq %%rdi, %%r12 \\ movq %%rdi, %%r12
\\ movq %%rsi, %%r13 \\ movq %%rsi, %%r13
@ -146,6 +149,8 @@ fn jumpstartNakedSysV() callconv(.Naked) void {
\\ jmpq *(%%r12) \\ jmpq *(%%r12)
\\ 0: \\ 0:
\\ \\
\\ popq %%r12
\\ popq %%r13
\\ popq %%rbp \\ popq %%rbp
\\ ret \\ ret
); );

View File

@ -6,6 +6,7 @@
// https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf // https://refspecs.linuxbase.org/elf/x86_64-abi-0.99.pdf
const std = @import("std"); const std = @import("std");
const tolmac = @import("../../tolmac.zig");
/// Used for stack parameter passing. /// Used for stack parameter passing.
pub const WordLimit = 128; pub const WordLimit = 128;
@ -31,7 +32,7 @@ fn determiteClass(comptime T: type, buffer: []Class) []Class {
0 => buffer[0] = .void, 0 => buffer[0] = .void,
1...64 => buffer[0] = .integer, 1...64 => buffer[0] = .integer,
65...128 => @compileError("unimplemented"), 65...128 => @compileError("unimplemented"),
else => @compileError("unimplemented"), else => @compileError("invalid sysv parameter"),
} }
}, },
.Float => |float| { .Float => |float| {
@ -40,16 +41,13 @@ fn determiteClass(comptime T: type, buffer: []Class) []Class {
1...64 => buffer[0] = .sse, 1...64 => buffer[0] = .sse,
65...80 => @compileError("unimplemented"), 65...80 => @compileError("unimplemented"),
81...128 => @compileError("unimplemented"), 81...128 => @compileError("unimplemented"),
else => @compileError("unimplemented"), else => @compileError("invalid sysv parameter"),
} }
}, },
.Bool => buffer[0] = .integer, .Bool => buffer[0] = .integer,
.Pointer => |ptr| { .Pointer => |ptr| {
switch (ptr.size) { switch (ptr.size) {
.Slice => { .Slice => @compileError("invalid sysv parameter"),
buffer[0] = .integer;
buffer[1] = .integer;
},
else => buffer[0] = .integer, else => buffer[0] = .integer,
} }
}, },
@ -69,24 +67,34 @@ pub fn generateOpZovSysvFromPrototype(prototype: anytype) !*const fn () callconv
// > The direction flag DF in the %rFLAGS register must be clear (set to forward // > The direction flag DF in the %rFLAGS register must be clear (set to forward
// > direction) on function entry and return. // > 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 { comptime {
const func = @typeInfo(@TypeOf(prototype)).Fn; const func = @typeInfo(@TypeOf(prototype)).Fn;
if (func.calling_convention != .SysV)
@compileError("Non SysV function passed");
var source_buffer = [_]u8{0} ** AsmBufferLimit; var source_buffer = [_]u8{0} ** AsmBufferLimit;
var source_needle: usize = 0; 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. // idea: Try using REP for big consequent memory pushes.
// todo: In-stack returns by pointing %rdi directly to final destination. // 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 = const Prelude =
\\ movq %%rsp, %%rbp # Move stack pointer in non-volatile %rbp to restore later \\ movq %%rsp, %%rbp # Move stack pointer in non-volatile %rbp to restore later
\\ subq $0x8, %%rsp \\ andq $-16, %%rsp # Align stack so that %rsp + 8 in callee is 16 aligned.
\\ subq $0x{x}, %%rsp
\\ \\
; ;
@ -102,9 +110,6 @@ 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; var integer_allocation: usize = 0;
const IntegerAllocations = [_][]const u8{ "rdi", "rsi", "rdx", "rcx", "r8", "r9", "stack" }; 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 sse_allocation: enum { xmm0, xmm1, xmm2, xmm3, xmm4, xmm5, xmm6, xmm7, stack } = .xmm0;
@ -118,6 +123,12 @@ pub fn generateOpZovSysvFromPrototype(prototype: anytype) !*const fn () callconv
parameter_stack_size += 8 * classes.len; 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. // Push parameters to appropriate registers and stack positions.
var stack_offset: usize = parameter_stack_size; var stack_offset: usize = parameter_stack_size;
for (func.params) |param| { for (func.params) |param| {
@ -160,3 +171,84 @@ pub fn generateOpZovSysvFromPrototype(prototype: anytype) !*const fn () callconv
}.op; }.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);
}

View File

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