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

AVR: u32 and u64 behaves like u16 and u32 #83281

Closed
ThisIsRex opened this issue Mar 18, 2021 · 3 comments · Fixed by rust-lang/compiler-builtins#462
Closed

AVR: u32 and u64 behaves like u16 and u32 #83281

ThisIsRex opened this issue Mar 18, 2021 · 3 comments · Fixed by rust-lang/compiler-builtins#462
Labels
C-bug Category: This is a bug. O-AVR Target: AVR processors (ATtiny, ATmega, etc.) requires-nightly This issue requires a nightly compiler in some way.

Comments

@ThisIsRex
Copy link

ThisIsRex commented Mar 18, 2021

I found out that i32 and u32 behaves like i16 and u16
And i64 and u64 like i32 and u32

I wrote the following code to represent the problem:

let mut counter_u32 = 0u32;
let mut counter_u64 = 0u64;
let mut counter_u64_2 = 0u64;

loop {
	delay_ms(1000);

	counter_u16 = counter_u16.wrapping_add(10000);
	counter_u32 = counter_u32.wrapping_add(10000);
	counter_u64 = counter_u64.wrapping_add(10000);
	counter_u64_2 = counter_u64_2.wrapping_add(1_000_000_000);

	ufmt::uwriteln!(&mut serial, "u16: {}", counter_u16).void_unwrap();
	ufmt::uwriteln!(&mut serial, "u32: {}", counter_u32).void_unwrap(); //overflow after 65535
	ufmt::uwriteln!(&mut serial, "u64>2^16?: {}", counter_u64 > 65535).void_unwrap();
	ufmt::uwriteln!(&mut serial, "u64>2^32?: {}", counter_u64 > 4294967295).void_unwrap(); //overflow after 4294967295

        //FLASH ERROR
        ufmt::uwriteln!(&mut serial, "u64: {}", counter_u64).void_unwrap();
}

Also if i'll write the code for displaying u64, avr-gcc can't flash the .elf file:

error: linking with `avr-gcc` failed: exit code: 1
  |
  = note: "avr-gcc" "-mmcu=atmega328p" "-Wl,--as-needed" "-L" "C:\\Users\\Admin\\.rustup\\toolchains\\nightly-2021-01-07-x86_64-pc-windows-msvc\\lib\\rustlib\\avr-atmega328p\\lib" "O:\\R\\Coding\\Rust\\test-proj\\target\\avr-atmega328p\\debug\\deps\\test-proj-6349af4f4221cee8.panic_halt-db131e4b002c16b4.panic_halt.7laydq1v-cgu.0.rcgu.o.rcgu.o" "-o" "O:\\R\\Coding\\Rust\\test-proj\\target\\avr-atmega328p\\debug\\deps\\test-proj-6349af4f4221cee8.elf" "-Wl,--gc-sections" "-L" "O:\\R\\Coding\\Rust\\test-proj\\target\\avr-atmega328p\\debug\\deps" "-L" "O:\\R\\Coding\\Rust\\test-proj\\target\\debug\\deps" "-L" "C:\\Users\\Admin\\.rustup\\toolchains\\nightly-2021-01-07-x86_64-pc-windows-msvc\\lib\\rustlib\\avr-atmega328p\\lib" "-Wl,--start-group" "-Wl,--end-group" "-Wl,-Bstatic" "O:\\R\\Coding\\Rust\\test-proj\\target\\avr-atmega328p\\debug\\deps\\libcompiler_builtins-e902a06b00d14d7b.rlib" "-Wl,-Bdynamic" "-lgcc"
  = note: O:\R\Coding\Rust\test-proj\target\avr-atmega328p\debug\deps\libcompiler_builtins-e902a06b00d14d7b.rlib(compiler_builtins-e902a06b00d14d7b.compiler_builtins.6kkuomye-cgu.7.rcgu.o): In function `compiler_builtins::int::shift::Ashl::ashl::h9dce3f4bd92dda5f':
          C:\Users\Admin\.cargo\registry\src\gh.neting.cc-1ecc6299db9ec823\compiler_builtins-0.1.36\src\int\shift.rs:(.text._ZN17compiler_builtins3int5shift4Ashl4ashl17h9dce3f4bd92dda5fE+0x150): undefined reference to `core::panicking::panic::hf53d0fffb6d21a9a'
          O:\R\Coding\Rust\test-proj\target\avr-atmega328p\debug\deps\libcompiler_builtins-e902a06b00d14d7b.rlib(compiler_builtins-e902a06b00d14d7b.compiler_builtins.6kkuomye-cgu.7.rcgu.o): In function `compiler_builtins::int::shift::Ashl::ashl::hceca1513a0a8cd5c':
          C:\Users\Admin\.cargo\registry\src\gh.neting.cc-1ecc6299db9ec823\compiler_builtins-0.1.36\src\int\shift.rs:(.text._ZN17compiler_builtins3int5shift4Ashl4ashl17hceca1513a0a8cd5cE+0x198): undefined reference to `core::panicking::panic::hf53d0fffb6d21a9a'
          O:\R\Coding\Rust\test-proj\target\avr-atmega328p\debug\deps\libcompiler_builtins-e902a06b00d14d7b.rlib(compiler_builtins-e902a06b00d14d7b.compiler_builtins.6kkuomye-cgu.7.rcgu.o): In function `compiler_builtins::int::shift::Lshr::lshr::h6706166a532fb8de':
          C:\Users\Admin\.cargo\registry\src\gh.neting.cc-1ecc6299db9ec823\compiler_builtins-0.1.36\src\int\shift.rs:(.text._ZN17compiler_builtins3int5shift4Lshr4lshr17h6706166a532fb8deE+0x1f8): undefined reference to `core::panicking::panic::hf53d0fffb6d21a9a'
          O:\R\Coding\Rust\test-proj\target\avr-atmega328p\debug\deps\libcompiler_builtins-e902a06b00d14d7b.rlib(compiler_builtins-e902a06b00d14d7b.compiler_builtins.6kkuomye-cgu.1.rcgu.o): In function `compiler_builtins::int::specialized_div_rem::u64_div_rem::h60f8b0912293273a':
          C:\Users\Admin\.cargo\registry\src\gh.neting.cc-1ecc6299db9ec823\compiler_builtins-0.1.36\src\int\specialized_div_rem\delegate.rs:(.text._ZN17compiler_builtins3int19specialized_div_rem11u64_div_rem17h60f8b0912293273aE+0x125c): undefined reference to `core::panicking::panic::hf53d0fffb6d21a9a'
          O:\R\Coding\Rust\test-proj\target\avr-atmega328p\debug\deps\libcompiler_builtins-e902a06b00d14d7b.rlib(compiler_builtins-e902a06b00d14d7b.compiler_builtins.6kkuomye-cgu.1.rcgu.o): In function `compiler_builtins::int::specialized_div_rem::u32_div_rem::h9023690cd40b9b7f':
          C:\Users\Admin\.cargo\registry\src\gh.neting.cc-1ecc6299db9ec823\compiler_builtins-0.1.36\src\int\specialized_div_rem\norm_shift.rs:(.text._ZN17compiler_builtins3int19specialized_div_rem11u32_div_rem17h9023690cd40b9b7fE+0x718): undefined reference to `core::panicking::panic::hf53d0fffb6d21a9a'
          collect2.exe: error: ld returned 1 exit status


error: aborting due to previous error; 38 warnings emitted

error: could not compile `test-proj`

I use nightly-2021-01-07 version because a newer one fails with Not supported instr error

@ThisIsRex ThisIsRex added the C-bug Category: This is a bug. label Mar 18, 2021
@jonas-schievink jonas-schievink added O-AVR Target: AVR processors (ATtiny, ATmega, etc.) requires-nightly This issue requires a nightly compiler in some way. labels Mar 18, 2021
@Rahix
Copy link

Rahix commented Mar 24, 2021

avr-gcc can't flash the .elf file

More precisely, it is failing to link the executable. This is a known problem between compiler-builtins and cargo build-std.

@drmorr0
Copy link
Contributor

drmorr0 commented Oct 30, 2021

I'm pretty sure this is a duplicate of #82380

@Patryk27
Copy link
Contributor

Patryk27 commented Dec 21, 2021

Seems like a duplicate of #82380, as in #82380 (comment) helps to avoid the linker issue and seems to generate a meaningful code; as for the overflow, unfortunately it's a problem in ufmt.

Patryk27 added a commit to Patryk27/compiler-builtins that referenced this issue May 15, 2022
For division and modulo, AVR uses a custom calling convention that does
not match compiler_builtins' expectations, leading to non-working code¹.

Ideally we'd just use hand-written naked functions (as, afair, ARM
does), but that's a lot of code to port², so hopefully we'll be able to
do it gradually later.

For the time being, I'd suggest not compiling problematic functions for
AVR target - this causes avr-gcc (which is a mandatory part of Rust+AVR
toolchain anyway) to link hand-written assembly from libgcc, which is
confirmed to work.

I've tested the code locally on simavr and the patch seems to be working
correctly :-)

¹ rust-lang/rust#82242,
  rust-lang/rust#83281
² https://github.com/gcc-mirror/gcc/blob/31048012db98f5ec9c2ba537bfd850374bdd771f/libgcc/config/avr/lib1funcs.S

Closes rust-lang/rust#82242
Closes rust-lang/rust#83281
Patryk27 added a commit to Patryk27/compiler-builtins that referenced this issue May 17, 2022
For division and modulo, AVR uses a custom calling convention that does
not match compiler_builtins' expectations, leading to non-working code¹.

Ideally we'd just use hand-written naked functions (as, afair, ARM
does), but that's a lot of code to port², so hopefully we'll be able to
do it gradually later.

For the time being, I'd suggest not compiling problematic functions for
AVR target - this causes avr-gcc (which is a mandatory part of Rust+AVR
toolchain anyway) to link hand-written assembly from libgcc, which is
confirmed to work.

I've tested the code locally on simavr and the patch seems to be working
correctly :-)

¹ rust-lang/rust#82242,
  rust-lang/rust#83281
² https://github.com/gcc-mirror/gcc/blob/31048012db98f5ec9c2ba537bfd850374bdd771f/libgcc/config/avr/lib1funcs.S

Closes rust-lang/rust#82242
Closes rust-lang/rust#83281
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. O-AVR Target: AVR processors (ATtiny, ATmega, etc.) requires-nightly This issue requires a nightly compiler in some way.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants