#Expected type '...' found 'comptime_int'

1 messages · Page 1 of 1 (latest)

turbid chasm
#

I'm trying to write a little register definition wrapper but am encountering compiler errors:

fn SysReg(comptime name: []const u8, comptime Val: anytype) type {
    return struct {
        // MSR Xt, <system_register> - Move the value in Xt to the system register
        pub fn set(val: Val) void {
            asm volatile ("msr " ++ name ++ "%[v]"
                :
                : [v] "{r}" (val),
            );
        }
    };
}

pub const SPSR_EL2 = SysReg("spsr_el2", packed struct(u64) {
    _res0: u32 = 0,
    _ignored: u21 = 0,
    D: u1 = 0,
    A: u1 = 0,
    I: u1 = 0,
    F: u1 = 0,
    _res0_5: u1 = 0,
    M: enum(u5) {
        EL0t = 0,
        EL1t = 4,
        EL1h = 5,
        EL2t = 8,
        EL2h = 9,
    } = 0,
});

test "compile failure" {
  SPSR_EL2.set(.{ .D = 1, .A = 1, .I = 1, .F = 1, .M = .EL1h });
}

Running this test produces the following compilation error and I can't figure out why

$zig test src/platform/aarch64/assembly.zig
src/platform/aarch64/registers.zig:13:48: error: expected type 'registers.SPSR_EL2__struct_3529.SPSR_EL2__struct_3529__enum_3532', found 'comptime_int'
pub const SPSR_EL2 = SysReg("spsr_el2", packed struct(u64) {
                                        ~~~~~~~^~~~~~
src/platform/aarch64/registers.zig:21:8: note: enum declared here
    M: enum(u5) {
       ^~~~
referenced by:
    SPSR_EL2: src/platform/aarch64/registers.zig:13:48
    test.compile failure: src/platform/aarch64/registers.zig:51:5
    remaining reference traces hidden; use '-freference-trace' to see all reference traces
real herald
#

I don't have any practice with packed structs yet, but for starters the parameter comptime Val: anytype probably aught to be comptime T: type

#

It doesn't solve your problem though, let me see

high minnow
#

You might need

real herald
#

I see, the problem is the default value of M. Change to this:

    M: enum(u5) {
        EL0t = 0,
        EL1t = 4,
        EL1h = 5,
        EL2t = 8,
        EL2h = 9,
    } = .EL0t,
high minnow
#

@enumToInt(enum_or_tagged_union: anytype) anytype

real herald
#

Once you change the param type to type, fix the default val of M and get the packed struct bit size to match total number of bits (64 vs. 63) this builds

turbid chasm
#

Oh nice, whoops

#

Thank you both 🙂