Skip to content

{.volatile.} is almost useless #3382

@rasendubi

Description

@rasendubi

As an embedded and kernel developer I need pointers to a volatile memory to work with memory-mapped hardware registers. Nim has a {.volatile.} pragma but it's a property of a variable.

let reg {.volatile.} = cast[ptr char](0x101f1000)

gives the following C output:

NIM_CHAR* volatile reg;
nimfr("kmain", "kernel.nim");
nimln(5, "kernel.nim");
reg = ((NIM_CHAR*) 270471168);

As you see, reg is a volatile pointer to a non-volatile memory. The behavior I want to achieve is a non-volatile pointer to a volatile memory:

NIM_CHAR volatile *reg;

I don't see a way to create such a variable in Nim. I would say, that's pretty critical: embedded and kernel-level programming is impossible without that.

As a fix I think of making volatile a property of a type rather than a variable. I expect to write the following:

let reg = cast[ptr[char {.volatile.}]](0x101f1000)
# or even
const reg = cast[ptr[char {.volatile.}]](0x101f1000)
# const reg {.volatile.} = ... doesn't even work now

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions