Skip to content

Register access is optimised away #7

Closed
@dylanmckay

Description

@dylanmckay

Issue #1 forces us to compile with optimisations turned off. Because of this, our outputted assembly is useless because LLVM has mangled all of our well thought out and placed register accessing.

The code

#![feature(lang_items)]
#![feature(macro_rules)]

#![no_std]

const __SFR_OFFSET: uint = 0x20;

/// Defines an AVR IO register.
/// See `avr/sfr_defs.h` in avr-libc.
macro_rules! avr_io_register {
    ($name:ident, $addr:expr) => {
        const $name: *mut u8 = ($addr + __SFR_OFFSET) as *mut u8;
    }
}

/// Defines an AVR register.
/// See `avr/sfr_defs.h` in avr-libc.
macro_rules! avr_mem_register {
    ($name:ident, $addr:expr) => {
        const $name: *mut u8 = $addr as *mut u8;
    }
}

// Port addresses taken from `avr/iom328p.h` in avr-libc.
avr_io_register!(DDRB, 0x04)
avr_io_register!(PORTB, 0x05)

#[start]
fn main(_: int, _: *const *const u8) -> int {

    unsafe {

        // set all port b pins as outputs (I'm too lazy to set this properly).
        *DDRB = 0b11111111;

        loop {

            // set all port b pins to low
            *PORTB = 0b00000000;

            // delay the program somehow.

            // set all port b pins to high
            *PORTB = 0b11111111;

            // delay the program somehow.
        }
    }
    0
}

// required for freestanding Rust.
#[lang="sized"]
trait Sized { }

Is processed by ./rustc ~/Desktop/avr_hello.rs --emit asm -O --target=avr-none and churns out:

    .text
    .file   "avr_hello.0.rs"
    .section    .text.main,"ax",@progbits
    .globl  main
    .align  2
    .type   main,@function
main:
    # load 0xFF into r24
    ldi r24, -1
    # store r24 in IO location 4
    out 4, r24
BB0_1:
    # go back to start, do not pass go, do not collect $200
    rjmp    BB0_1
.Ltmp0:
    .size   main, .Ltmp0-main

Which simply sets DDRB and then executes an empty infinite loop.

This can be fixed by fixing #4 or implementing #6, or alleviated by fixing #1 (so we could at least compile without optimisations).

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