Skip to content

Commit

Permalink
Use extern.
Browse files Browse the repository at this point in the history
  • Loading branch information
fubark committed Aug 31, 2023
1 parent 46e3f9f commit 7870ab6
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/bytecode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,9 @@ pub const StringIndexInsertContext = struct {
}
};

pub const Const = packed union {
pub const Const = extern union {
val: u64,
two: packed struct {
two: extern struct {
lower: u32,
upper: u32,
},
Expand Down
11 changes: 8 additions & 3 deletions src/chunk.zig
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const std = @import("std");
const stdx = @import("stdx");
const t = stdx.testing;
const builtin = @import("builtin");
const cy = @import("cyber.zig");
const fmt = cy.fmt;
Expand Down Expand Up @@ -714,15 +715,19 @@ pub const Chunk = struct {
}
};

const DataNode = packed struct {
inner: packed union {
funcSym: packed struct {
const DataNode = extern struct {
inner: extern union {
funcSym: extern struct {
symId: u32,
},
},
next: u32,
};

test "Internals." {
try t.eq(@sizeOf(DataNode), 8);
}

const GenBlock = struct {
/// This includes the return info, function params, captured params, and local vars.
/// Does not include temp locals.
Expand Down
2 changes: 1 addition & 1 deletion src/debug.zig
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub fn buildStackTrace(self: *cy.VM) !void {
if (sym.frameLoc == cy.NullId) {
break;
} else {
pcOffset = cy.getInstOffset(self, self.stack[fpOffset + 2].retPcPtr) - self.stack[fpOffset + 1].retInfo.callInstOffset;
pcOffset = cy.getInstOffset(self, self.stack[fpOffset + 2].retPcPtr) - self.stack[fpOffset + 1].retInfoCallInstOffset();
fpOffset = cy.getStackOffset(self, self.stack[fpOffset + 3].retFramePtr);
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/fiber.zig
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn releaseFiberStack(vm: *cy.VM, fiber: *cy.Fiber) !void {

// Unwind stack and release all locals.
while (framePtr > 0) {
pc = @intCast(getInstOffset(vm.ops.ptr, stack[framePtr + 2].retPcPtr) - stack[framePtr + 1].retInfo.callInstOffset);
pc = @intCast(getInstOffset(vm.ops.ptr, stack[framePtr + 2].retPcPtr) - stack[framePtr + 1].retInfoCallInstOffset());

// Compute next frame ptr offset.
framePtr = @intCast(getStackOffset(stack.ptr, stack[framePtr + 3].retFramePtr));
Expand Down Expand Up @@ -206,7 +206,7 @@ pub fn unwindReleaseStack(vm: *cy.VM, stack: []const Value, startFramePtr: [*]co
return;
} else {
// Unwind.
pcOffset = getInstOffset(vm.ops.ptr, stack[fpOffset + 2].retPcPtr) - stack[fpOffset + 1].retInfo.callInstOffset;
pcOffset = getInstOffset(vm.ops.ptr, stack[fpOffset + 2].retPcPtr) - stack[fpOffset + 1].retInfoCallInstOffset();
fpOffset = getStackOffset(stack.ptr, stack[fpOffset + 3].retFramePtr);
}
}
Expand Down Expand Up @@ -240,7 +240,7 @@ pub fn unwindThrowUntilFramePtr(vm: *cy.VM, startFp: [*]const Value, pc: [*]cons
});

// Unwind frame.
pcOffset = getInstOffset(vm.ops.ptr, vm.stack[fpOffset + 2].retPcPtr) - vm.stack[fpOffset + 1].retInfo.callInstOffset;
pcOffset = getInstOffset(vm.ops.ptr, vm.stack[fpOffset + 2].retPcPtr) - vm.stack[fpOffset + 1].retInfoCallInstOffset();
fpOffset = getStackOffset(vm.stack.ptr, vm.stack[fpOffset + 3].retFramePtr);
}

Expand Down
2 changes: 1 addition & 1 deletion src/runtime.zig
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ pub const FuncSymbolEntry = extern struct {
} = undefined,
inner: extern union {
nativeFunc1: cy.NativeFuncPtr,
func: packed struct {
func: extern struct {
pc: u32,
/// Stack size required by the func.
stackSize: u16,
Expand Down
4 changes: 3 additions & 1 deletion src/sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub const LocalVar = struct {
}
};

pub const CapVarDesc = packed union {
pub const CapVarDesc = extern union {
/// The user of a captured var contains the SemaVarId back to the owner's var.
user: LocalVarId,
};
Expand Down Expand Up @@ -4346,4 +4346,6 @@ test "Internals." {
try t.eq(@sizeOf(ResolvedSymData), 12);
try t.eq(@sizeOf(Name), 16);
try t.eq(@sizeOf(CompactResolvedSymId), 4);

try t.eq(@sizeOf(CapVarDesc), 4);
}
39 changes: 26 additions & 13 deletions src/value.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,16 +66,17 @@ pub const Value = packed union {
val: u64,

/// Call frame return info.
retInfo: packed struct {
numRetVals: u8,
retFlag: u8,

/// Since there are different call insts with varying lengths,
/// the call convention prefers to advance the pc before saving it so
/// stepping over the call will already have the correct pc.
/// An offset is stored to the original call inst for stack unwinding.
callInstOffset: u8,
},
// retInfo: packed struct {
// numRetVals: u8,
// retFlag: u8,

// /// Since there are different call insts with varying lengths,
// /// the call convention prefers to advance the pc before saving it so
// /// stepping over the call will already have the correct pc.
// /// An offset is stored to the original call inst for stack unwinding.
// callInstOffset: u8,
// },

retPcPtr: [*]const cy.Inst,
retFramePtr: [*]Value,
// two: packed struct {
Expand All @@ -91,6 +92,18 @@ pub const Value = packed union {
/// Returned from native funcs.
pub const Interrupt = Value{ .val = ErrorMask | (@as(u32, 0xFF) << 8) | std.math.maxInt(u8) };

pub inline fn retInfoCallInstOffset(self: *const Value) u8 {
return @intCast((self.val & 0xff0000) >> 16);
}

pub inline fn retInfoRetFlag(self: *const Value) u8 {
return @intCast((self.val & 0xff00) >> 8);
}

pub inline fn retInfoNumRet(self: *const Value) u8 {
return @intCast(self.val & 0xff);
}

pub inline fn asInteger(self: *const Value) i32 {
return @bitCast(@as(u32, @intCast(self.val & 0xffffffff)));
}
Expand Down Expand Up @@ -675,7 +688,7 @@ test "Internals." {

// Check Zig/C struct compat.
try t.eq(@sizeOf(Value), @sizeOf(vmc.Value));
const retInfoT = std.meta.fieldInfo(Value, .retInfo).type;
try t.eq(@offsetOf(retInfoT, "numRetVals"), 0);
try t.eq(@offsetOf(retInfoT, "retFlag"), 1);
// const retInfoT = std.meta.fieldInfo(Value, .retInfo).type;
// try t.eq(@offsetOf(retInfoT, "numRetVals"), 0);
// try t.eq(@offsetOf(retInfoT, "retFlag"), 1);
}
38 changes: 22 additions & 16 deletions src/vm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3784,8 +3784,8 @@ fn evalLoop(vm: *VM) linksection(cy.HotSection) error{StackOverflow, OutOfMemory
}

fn popStackFrameLocal0(pc: *[*]const cy.Inst, framePtr: *[*]Value) linksection(cy.HotSection) bool {
const retFlag = framePtr.*[1].retInfo.retFlag;
const reqNumArgs = framePtr.*[1].retInfo.numRetVals;
const retFlag = framePtr.*[1].retInfoRetFlag();
const reqNumArgs = framePtr.*[1].retInfoNumRet();
if (reqNumArgs == 0) {
pc.* = framePtr.*[2].retPcPtr;
framePtr.* = framePtr.*[3].retFramePtr;
Expand Down Expand Up @@ -3814,8 +3814,8 @@ fn popStackFrameLocal0(pc: *[*]const cy.Inst, framePtr: *[*]Value) linksection(c
}

fn popStackFrameLocal1(vm: *VM, pc: *[*]const cy.Inst, framePtr: *[*]Value) linksection(cy.HotSection) bool {
const retFlag = framePtr.*[1].retInfo.retFlag;
const reqNumArgs = framePtr.*[1].retInfo.numRetVals;
const retFlag = framePtr.*[1].retInfoRetFlag();
const reqNumArgs = framePtr.*[1].retInfoNumRet();
if (reqNumArgs == 1) {
pc.* = framePtr.*[2].retPcPtr;
framePtr.* = framePtr.*[3].retFramePtr;
Expand Down Expand Up @@ -4323,24 +4323,30 @@ fn callMethodGroupNoInline(

pub inline fn buildReturnInfo2(numRetVals: u8, comptime cont: bool, comptime callInstOffset: u8) Value {
return .{
.retInfo = .{
.numRetVals = numRetVals,
// .retFlag = if (cont) 0 else 1,
.retFlag = @intFromBool(!cont),
.callInstOffset = callInstOffset,
},
.val = numRetVals | (@as(u32, @intFromBool(!cont)) << 8) | (@as(u32, callInstOffset) << 16),
};
// return .{
// .retInfo = .{
// .numRetVals = numRetVals,
// // .retFlag = if (cont) 0 else 1,
// .retFlag = @intFromBool(!cont),
// .callInstOffset = callInstOffset,
// },
// };
}

pub inline fn buildReturnInfo(comptime numRetVals: u2, comptime cont: bool, comptime callInstOffset: u8) Value {
return .{
.retInfo = .{
.numRetVals = numRetVals,
// .retFlag = if (cont) 0 else 1,
.retFlag = @intFromBool(!cont),
.callInstOffset = callInstOffset,
},
.val = numRetVals | (@as(u32, @intFromBool(!cont)) << 8) | (@as(u32, callInstOffset) << 16),
};
// return .{
// .retInfo = .{
// .numRetVals = numRetVals,
// // .retFlag = if (cont) 0 else 1,
// .retFlag = @intFromBool(!cont),
// .callInstOffset = callInstOffset,
// },
// };
}

pub inline fn getInstOffset(vm: *const VM, to: [*]const cy.Inst) u32 {
Expand Down

0 comments on commit 7870ab6

Please sign in to comment.