Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move code to the 2018 edition #19

Merged
merged 2 commits into from
Sep 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ci/exceptions/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
authors = ["Jorge Aparicio <jorge@japaric.io>"]
edition = "2018"
name = "app"
version = "0.1.0"
authors = ["Jorge Aparicio <jorge@japaric.io>"]

[dependencies]
rt = { path = "../rt" }
5 changes: 2 additions & 3 deletions ci/exceptions/app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
#![no_main]
#![no_std]

#[macro_use]
extern crate rt;

use core::intrinsics;

use rt::entry;

entry!(main);

fn main() -> ! {
Expand Down
5 changes: 2 additions & 3 deletions ci/exceptions/app2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
#![no_main]
#![no_std]

#[macro_use]
extern crate rt;

use core::intrinsics;

use rt::entry;

entry!(main);

fn main() -> ! {
Expand Down
3 changes: 1 addition & 2 deletions ci/exceptions/rt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(panic_handler)]
#![no_std]

use core::panic::PanicInfo;
Expand Down Expand Up @@ -37,7 +36,7 @@ pub unsafe extern "C" fn Reset() -> ! {
pub static RESET_VECTOR: unsafe extern "C" fn() -> ! = Reset;

#[panic_handler]
fn panic(_panic: &PanicInfo) -> ! {
fn panic(_panic: &PanicInfo<'_>) -> ! {
loop {}
}

Expand Down
1 change: 1 addition & 0 deletions ci/main/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
[package]
edition = "2018"
name = "app"
version = "0.1.0"
authors = ["Jorge Aparicio <jorge@japaric.io>"]
Expand Down
3 changes: 1 addition & 2 deletions ci/main/app2/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![no_std]
#![no_main]

#[macro_use]
extern crate rt;
use rt::entry;

entry!(main);

Expand Down
3 changes: 1 addition & 2 deletions ci/main/app3/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![no_std]
#![no_main]

#[macro_use]
extern crate rt;
use rt::entry;

entry!(main);

Expand Down
3 changes: 2 additions & 1 deletion ci/main/app4/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
[package]
authors = ["Jorge Aparicio <jorge@japaric.io>"]
edition = "2018"
name = "app"
version = "0.1.0"
authors = ["Jorge Aparicio <jorge@japaric.io>"]

[dependencies]
rt = { path = "../rt2" }
5 changes: 2 additions & 3 deletions ci/main/app4/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
#![no_main]
#![no_std]

#[macro_use]
extern crate rt;

use core::ptr;

use rt::entry;

entry!(main);

static mut DATA: i32 = 1;
Expand Down
3 changes: 2 additions & 1 deletion ci/main/rt/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[package]
name = "rt"
edition = "2018"
name = "rt" # <-
version = "0.1.0"
authors = ["Jorge Aparicio <jorge@japaric.io>"]

Expand Down
3 changes: 1 addition & 2 deletions ci/main/rt/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(panic_handler)]
#![no_std]

use core::panic::PanicInfo;
Expand All @@ -19,7 +18,7 @@ pub unsafe extern "C" fn Reset() -> ! {
pub static RESET_VECTOR: unsafe extern "C" fn() -> ! = Reset;

#[panic_handler]
fn panic(_panic: &PanicInfo) -> ! {
fn panic(_panic: &PanicInfo<'_>) -> ! {
loop {}
}

Expand Down
3 changes: 1 addition & 2 deletions ci/main/rt2/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(panic_handler)]
#![no_std]

use core::panic::PanicInfo;
Expand Down Expand Up @@ -37,7 +36,7 @@ pub unsafe extern "C" fn Reset() -> ! {
pub static RESET_VECTOR: unsafe extern "C" fn() -> ! = Reset;

#[panic_handler]
fn panic(_panic: &PanicInfo) -> ! {
fn panic(_panic: &PanicInfo<'_>) -> ! {
loop {}
}

Expand Down
3 changes: 1 addition & 2 deletions ci/memory-layout/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![feature(panic_handler)]
#![no_main]
#![no_std]

Expand All @@ -19,6 +18,6 @@ pub unsafe extern "C" fn Reset() -> ! {
pub static RESET_VECTOR: unsafe extern "C" fn() -> ! = Reset;

#[panic_handler]
fn panic(_panic: &PanicInfo) -> ! {
fn panic(_panic: &PanicInfo<'_>) -> ! {
loop {}
}
16 changes: 16 additions & 0 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ main() {
diff app.o.nm \
<(cargo nm -- target/thumbv7m-none-eabi/debug/deps/app-*.o | grep '[0-9]* [^n] ')

edition_check

popd

# # memory-layout
Expand All @@ -38,6 +40,8 @@ main() {

qemu_check target/thumbv7m-none-eabi/debug/app

edition_check

popd

# # main
Expand All @@ -47,20 +51,25 @@ main() {
pushd app
diff app.objdump \
<(cargo objdump --bin app -- -d -no-show-raw-insn)
# disabled because of rust-lang/rust#53964
# edition_check
popd

# check that it builds
pushd app2
cargo build
edition_check
popd

pushd app3
cargo build
edition_check
popd

pushd app4
cargo build
qemu_check target/thumbv7m-none-eabi/debug/app
edition_check
popd

popd
Expand All @@ -72,16 +81,23 @@ main() {
pushd app
diff app.vector_table.objdump \
<(cargo objdump --bin app --release -- -s -j .vector_table)
edition_check
popd

# check that it builds
pushd app2
cargo build
edition_check
popd

popd
}

# checks that 2018 idioms are being used
edition_check() {
RUSTFLAGS="-D rust_2018_compatibility -D rust_2018_idioms" cargo check
}

# checks that QEMU doesn't crash and that it produces no error messages
qemu_check() {
qemu-system-arm \
Expand Down
3 changes: 2 additions & 1 deletion ci/smallest-no-std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[package]
authors = ["Jorge Aparicio <jorge@japaric.io>"]
edition = "2018"
name = "app"
authors = ["Jorge Aparicio <jorge@japaric.io>"]
version = "0.1.0"

[dependencies]
3 changes: 1 addition & 2 deletions ci/smallest-no-std/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#![feature(panic_handler)]
#![no_main]
#![no_std]

use core::panic::PanicInfo;

#[panic_handler]
fn panic(_panic: &PanicInfo) -> ! {
fn panic(_panic: &PanicInfo<'_>) -> ! {
loop {}
}
45 changes: 27 additions & 18 deletions src/exceptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ The first thing we'll do is create an array of vectors (pointers to exception ha
`rt` crate's code:

``` console
$ sed -n 57,92p ../rt/src/lib.rs
$ sed -n 56,91p ../rt/src/lib.rs
```

``` rust
{{#include ../ci/exceptions/rt/src/lib.rs:57:92}}
{{#include ../ci/exceptions/rt/src/lib.rs:56:91}}
```

Some of the entries in the vector table are *reserved*; the ARM documentation states that they
Expand All @@ -63,7 +63,7 @@ $ tail -n4 ../rt/src/lib.rs
```

``` rust
{{#include ../ci/exceptions/rt/src/lib.rs:94:97}}
{{#include ../ci/exceptions/rt/src/lib.rs:93:97}}
```

## Linker script side
Expand Down Expand Up @@ -105,24 +105,31 @@ handler for the respective exception.
That's it! The `rt` crate now has support for exception handlers. We can test it out with following
application:

> **NOTE**: Turns out it's hard to generate an exception in QEMU. On real
> hardware a read to an invalid memory address (i.e. outside of the Flash and
> RAM regions) would be enough but QEMU happily accepts the operation and
> returns zero. A trap instruction works on both QEMU and hardware but
> unfortunately it's not available on stable so you'll have to temporarily
> switch to nightly to run this and the next example.

``` rust
{{#include ../ci/exceptions/app/src/main.rs}}
```

``` console
(lldb) b DefaultExceptionHandler
Breakpoint 1: where = app`DefaultExceptionHandler at lib.rs:96, address = 0x000000ec
Breakpoint 1: where = app`DefaultExceptionHandler at lib.rs:95, address = 0x000000ec

(lldb) continue
Process 1 resuming
Process 1 stopped
* thread #1, stop reason = breakpoint 1.1
frame #0: 0x000000ec app`DefaultExceptionHandler at lib.rs:96
93
94 #[no_mangle]
95 pub extern "C" fn DefaultExceptionHandler() {
-> 96 loop {}
97 }
frame #0: 0x000000ec app`DefaultExceptionHandler at lib.rs:95
92
93 #[no_mangle]
94 pub extern "C" fn DefaultExceptionHandler() {
-> 95 loop {}
96 }
```

And for completeness, here's the disassembly of the optimized version of the program:
Expand Down Expand Up @@ -220,12 +227,12 @@ You can test it in QEMU
Process 1 resuming
Process 1 stopped
* thread #1, stop reason = breakpoint 1.1
frame #0: 0x00000044 app`HardFault at main.rs:19
16 #[no_mangle]
17 pub extern "C" fn HardFault() -> ! {
18 // do something interesting here
-> 19 loop {}
20 }
frame #0: 0x00000044 app`HardFault at main.rs:18
15 #[no_mangle]
16 pub extern "C" fn HardFault() -> ! {
17 // do something interesting here
-> 18 loop {}
19 }
```

The program now executes the user defined `HardFault` function instead of the
Expand All @@ -234,6 +241,8 @@ The program now executes the user defined `HardFault` function instead of the
Like our first attempt at a `main` interface, this first implementation has the problem of having no
type safety. It's also easy to mistype the name of the exception, but that doesn't produce an error
or warning. Instead the user defined handler is simply ignored. Those problems can be fixed using a
macro like the [`exception!`] macro defined in `cortex-m-rt`.
macro like the [`exception!`] macro defined in `cortex-m-rt` v0.5.x or the
[`exception`] attribute in `cortex-m-rt` v0.6.x.

[`exception!`]: https://github.com/japaric/cortex-m-rt/blob/v0.5.1/src/lib.rs#L79
[`exception!`]: https://github.com/japaric/cortex-m-rt/blob/v0.5.1/src/lib.rs#L792
[`exception`]: https://github.com/rust-embedded/cortex-m-rt/blob/v0.6.3/macros/src/lib.rs#L254
14 changes: 7 additions & 7 deletions src/main.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ And then rename it to `rt` which stands for "runtime".
``` console
$ sed -i s/app/rt/ Cargo.toml

$ head -n2 Cargo.toml
$ head -n4 Cargo.toml
```

``` toml
{{#include ../ci/main/rt/Cargo.toml:1:2}}
{{#include ../ci/main/rt/Cargo.toml:1:4}}
```

The first change is to have the reset handler call an external `main` function:

``` console
$ head -n14 src/lib.rs
$ head -n13 src/lib.rs
```

``` rust
{{#include ../ci/main/rt/src/lib.rs:1:14}}
{{#include ../ci/main/rt/src/lib.rs:1:13}}
```

We also drop the `#![no_main]` attribute has it has no effect on library crates.
Expand Down Expand Up @@ -63,7 +63,7 @@ The `rt` will take care of giving the program the right memory layout.
``` console
$ cd ..

$ cargo new --bin app
$ cargo new --edition 2018 --bin app

$ cd app

Expand Down Expand Up @@ -104,7 +104,7 @@ $ tail -n12 ../rt/src/lib.rs
```

``` rust
{{#include ../ci/main/rt/src/lib.rs:26:37}}
{{#include ../ci/main/rt/src/lib.rs:25:37}}
```

Then the application writers can invoke it like this:
Expand Down Expand Up @@ -215,7 +215,7 @@ $ head -n32 ../rt/src/lib.rs
```

``` rust
{{#include ../ci/main/rt2/src/lib.rs:1:32}}
{{#include ../ci/main/rt2/src/lib.rs:1:31}}
```

Now end users can directly and indirectly make use of `static` variables without running into
Expand Down
Loading