Ya, it could be possible to implement a few bits of this as assembly or 
primitive llvm instructions. My only worry would be how the optimizer interacts 
with some of those statements. If it sees what it thinks is an infinite loop it 
might optimize out something important, I haven't invested to much time into 
this yet as I do not think the runtime works on ARM platforms yet, I know a few 
bits of rust are still actively changing (like vectors and the addition of 
classes). It is something I would be interested in looking into in the future 
however.

The common case in embedded is something like:

int PIN1 = address 0x4000; // MMIO Pin

while (1) {
    if (PIN1 == HIGH) { do_something(); }
}

And if the value of the PIN1 is set to 0/1 initially and not set as volatile 
then a compiler can recognize that the value will never change, so the if's 
body is dead code.

With an explicit load instruction you can do something like:

int PIN1 = address 0x4000; // MMIO Pin

while (1) {
    val pin1_state = volatile_load(PIN1);
    if (pin1_state == HIGH) { do_something; }
}

Which is not that different, just more explicit, but again I do not know how 
the optimizer would interact with that kind of a statement. It would be nice to 
be able to mark an expression as volatile possibly instead of a variable. 

Possible something like:

let PIN1: address = 0x4000;

loop {
    if volatile *PIN1 == 1 { do_something; }
}

This could be done entirely in llvm-ir or assembly, so maybe I should just 
argue for an asm/llvm-ir syntax extension. 
 
--
John Harrison

On May 4, 2012, at 11:58 AM, Joe Groff wrote:

> On Fri, May 4, 2012 at 10:56 AM, John Harrison
> <[email protected]> wrote:
>> I understand that most people do not understand the volatile keyword and it 
>> is very commonly misused, but it does serve a very important purpose in 
>> embedded and low level programming. I could always try to write anything 
>> that requires MMIO or signals in C and just call it from rust, but that 
>> feels unsatisfactory.
> 
> An easy stopgap might be to add unsafe primitives for volatile_load
> and volatile_store that map to LLVM load volatile/store volatile
> instructions.
> 
> -Joe
> 


_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to