{.volatile.} is almost useless #3382

rasendubi opened this Issue Sep 27, 2015 · 4 comments


None yet

4 participants


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

As a quick and dirty workaround you can use emit.

{.emit: "NIM_CHAR volatile* myPtr;".}
var myPtr {.nodecl, importc.}: ptr char
myPtr[] = 'A'

I guess this could be wrapped in a macro.


The other solution (from Rust: rust-lang/rust#11172) is to implement volatile_load and volatile_store functions.

Araq commented Sep 28, 2015

Volatile is not a property of a type and should not be.

Araq commented Sep 29, 2015

volatile_load and volatile_store is the superior solution indeed.

@dom96 dom96 added the Feature label Jan 16, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment