Skip to content

Bug: "unable to evaluate constant expression" when depending on symbol address #6789

Closed
@ikskuh

Description

@ikskuh

Consider the following Zig code:

var stack: [256]u32 = undefined;

export const interrupt_table = [_][*]u32{
    @ptrCast([*]u32, &stack) + stack.len,
};

which will yield this error:

<source>:4:30: error: unable to evaluate constant expression
    @ptrCast([*]u32, &stack) + stack.len,
                             ^
<source>:3:41: note: referenced here
export const interrupt_table = [_][*]u32{
                                        ^

Such code is required when bootstrapping microcontrolls like the Cortex M3, as it requires the initial stack pointer to be stored in the interrupt vector table.

The C equivalent works:

uint32_t stack[256];

const uint32_t * interrupt_table[1] = {
    stack + 256,
};

Example in godbolt: https://c.godbolt.org/z/zhxcMM

One workaround i tried was declaring the end of stack as a separate symbol:

extern var stack: [256]u32;

export const interrupt_table = [_][*]u32{
    &stack,
};

but this yields this error:

<source>:4:5: error: unable to evaluate constant expression
    &stack,
    ^
<source>:3:41: note: referenced here
export const interrupt_table = [_][*]u32{
                                        ^

This code is required to work, otherwise it is not possible to bootstrap such microcontrollers in Zig.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorstage1The process of building from source via WebAssembly and the C backend.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions