Zig 0.17.0-dev (Split by item)

This is an example of documentation generated by ZigDoc, an alternative to Zig's built-in Auto Doc feature. See also examples in other modes/formats. The project being documented here (as the example) is the Zig library itself.

X86

cpu_context.X86
const X86 = struct

File

lib/std/debug/cpu_context.zig:1769

Code

const X86 = struct {
    gprs: std.enums.EnumArray(GprName, Gpr),

    /// The first 8 registers here intentionally match the order of registers in the x86 instruction
    /// encoding. This order is inherited by the PUSHA instruction and the DWARF register mappings,
    /// among other things.
    pub const GprName = enum {
        // zig fmt: off
        eax, ecx, edx, ebx,
        esp, ebp, esi, edi,
        eip,
        // zig fmt: on
    };
    pub const Gpr = u32;

    pub inline fn current() X86 {
        var ctx: X86 = undefined;
        asm volatile (
            \\ movl %%eax, 0x00(%%edi)
            \\ movl %%ecx, 0x04(%%edi)
            \\ movl %%edx, 0x08(%%edi)
            \\ movl %%ebx, 0x0c(%%edi)
            \\ movl %%esp, 0x10(%%edi)
            \\ movl %%ebp, 0x14(%%edi)
            \\ movl %%esi, 0x18(%%edi)
            \\ movl %%edi, 0x1c(%%edi)
            \\ call 1f
            \\1:
            \\ popl 0x20(%%edi)
            :
            : [gprs] "{edi}" (&ctx.gprs.values),
            : .{ .memory = true });
        return ctx;
    }

    pub fn getFp(ctx: *const X86) usize {
        return ctx.gprs.get(.ebp);
    }
    pub fn getPc(ctx: *const X86) usize {
        return ctx.gprs.get(.eip);
    }

    pub fn dwarfRegisterBytes(ctx: *X86, register_num: u16) DwarfRegisterError![]u8 {
        // System V Application Binary Interface Intel386 Architecture Processor Supplement Version 1.1
        //   ยง 2.4.2 "DWARF Register Number Mapping"
        switch (register_num) {
            // The order of `Gpr` intentionally matches DWARF's mappings.
            //
            // x86-macos sometimes uses different mappings (ebp and esp are reversed when the unwind
            // information is from `__eh_frame`). This deviation is not considered here, because
            // x86-macos is a deprecated target which is not supported by the Zig Standard Library.
            0...8 => return @ptrCast(&ctx.gprs.values[register_num]),

            9 => return error.UnsupportedRegister, // eflags
            11...18 => return error.UnsupportedRegister, // st0 - st7
            21...28 => return error.UnsupportedRegister, // xmm0 - xmm7
            29...36 => return error.UnsupportedRegister, // mm0 - mm7
            39 => return error.UnsupportedRegister, // mxcsr
            40...45 => return error.UnsupportedRegister, // es, cs, ss, ds, fs, gs
            48 => return error.UnsupportedRegister, // tr
            49 => return error.UnsupportedRegister, // ldtr
            93...100 => return error.UnsupportedRegister, // k0 - k7 (AVX-512)

            else => return error.InvalidRegister,
        }
    }
}