<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/98779>98779</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Trying to save a value in an memory mapped device is resulting in a wrong address. (RISC-V 32)
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
AlvaroEFMota
</td>
</tr>
</table>
<pre>
Hello, I am trying to use the Rust compiler to generate a assembly code to run on the [cpulator](https://cpulator.01xz.net/?sys=rv32-de1soc) which have a display mapped at `0xff200020`. The code is the smallest possible. I'm just trying to save the value `10` (a random value) at position `0xff200020` where I can control a mini display.
`main.rs`
```
#![no_std]
#![no_main]
use core::panic::PanicInfo;
const DEVICE_BASE_ADDRESS: usize = 0xff200200;
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop {}
}
#[no_mangle]
pub extern "C" fn _start() -> ! {
let device_ptr = DEVICE_BASE_ADDRESS as *mut usize;
let c = 10;
unsafe {
*device_ptr = c;
}
loop {}
}
```
With the command `rustc --target riscv32imac-unknown-none-elf -C panic=abort main.rs --emit=llvm-ir`, the generated llvm-ir is
```
ModuleID = 'main.e1e2a74214aae430-cgu.0'
source_filename = "main.e1e2a74214aae430-cgu.0"
target datalayout = "e-m:e-p:32:32-i64:64-n32-S128"
target triple = "riscv32"
; Function Attrs: noreturn nounwind
define hidden void @rust_begin_unwind(ptr align 4 %_info) unnamed_addr #0 {
start:
br label %bb1
bb1: ; preds = %bb1, %start
br label %bb1
}
; Function Attrs: noreturn nounwind
define dso_local void @_start() unnamed_addr #0 {
start:
store i32 10, ptr inttoptr (i32 -14679552 to ptr), align 4
br label %bb1
bb1: ; preds = %bb1, %start
br label %bb1
}
attributes #0 = { noreturn nounwind "target-cpu"="generic-rv32" "target-features"="+m,+a,+c" }
!llvm.ident = !{!0}
!0 = !{!"rustc version 1.79.0 (129f3b996 2024-06-10)"}
```
The line `store i32 10, ptr inttoptr (i32 -14679552 to ptr), align 4` store the value `10` in the correct address `-14679552` (`0xff200020`). but in the generated assembly it is not what happen.
To generate the assembly I used the command `rustc --target riscv32imac-unknown-none-elf -C panic=abort main.rs --emit=asm`
```
.text
.attribute 4, 16
.attribute 5, "rv32i2p1_m2p0_a2p1_c2p0"
.file "main.e1e2a74214aae430-cgu.0"
.section .text.rust_begin_unwind,"ax",@progbits
.hidden rust_begin_unwind
.globl rust_begin_unwind
.p2align 1
.type rust_begin_unwind,@function
rust_begin_unwind:
j .LBB0_1
.LBB0_1:
j .LBB0_1
.Lfunc_end0:
.size rust_begin_unwind, .Lfunc_end0-rust_begin_unwind
.section .text._start,"ax",@progbits
.globl _start
.p2align 1
.type _start,@function
_start:
lui a1, 1044992
li a0, 10
sw a0, 512(a1)
j .LBB1_1
.LBB1_1:
j .LBB1_1
.Lfunc_end1:
.size _start, .Lfunc_end1-_start
.ident "rustc version 1.79.0 (129f3b996 2024-06-10)"
.section ".note.GNU-stack","",@progbits
```
The line `lui a1, 1044992` (load the data in the first 20 bits of register `a1`) is loading `0xff200000` in the register `a1`, and the value `10` stored in `a0` is loaded to `0xff200000` with offset of `512` which is `0x200`. So the value is stored in `0xff200200` ,but not in `0xff200020`. The correct value would be `32` insted of `512` in `sw a0, 512(a1)`.
I used the llvm compiler in the generated llvm-ir with the command `llc -march=riscv32 main.ll -o test`, but it have the same offset `sw a1, 512(a0)`.
```
.text
.attribute 4, 16
.attribute 5, "rv32i2p1"
.file "main.e1e2a74214aae430-cgu.0"
.option push
.option arch, +a, +c, +m
.hidden rust_begin_unwind # -- Begin function rust_begin_unwind
.globl rust_begin_unwind
.p2align 1
.type rust_begin_unwind,@function
rust_begin_unwind: # @rust_begin_unwind
# %bb.0: # %start
.LBB0_1: # %bb1
# =>This Inner Loop Header: Depth=1
j .LBB0_1
.Lfunc_end0:
.size rust_begin_unwind, .Lfunc_end0-rust_begin_unwind
# -- End function
.option pop
.option push
.option arch, +a, +c, +m
.globl _start # -- Begin function _start
.p2align 1
.type _start,@function
_start: # @_start
# %bb.0: # %start
lui a0, 1044992
li a1, 10
sw a1, 512(a0)
.LBB1_1: # %bb1
# =>This Inner Loop Header: Depth=1
j .LBB1_1
.Lfunc_end1:
.size _start, .Lfunc_end1-_start
# -- End function
.option pop
.ident "rustc version 1.79.0 (129f3b996 2024-06-10)"
.section ".note.GNU-stack","",@progbits
```
I modified the generated assembly code on [cpulator](https://cpulator.01xz.net/?sys=rv32-de1soc) (I removed some headers) and the code worked (it show a random printing on the display because I don't know how the display works yet)
```
.global _start
_start:
lui a0, 1044992
li a1, 10
sw a1, 32(a0)
```
If you click on compile button you will see this
![Screenshot from 2024-07-13 21-30-06](https://github.com/user-attachments/assets/e2e1e402-3c62-4b00-8f61-cba3ad9025ac)
If you don't correct this line of code and use `sw a1, 512(a0)` you will get this error
![Screenshot from 2024-07-13 21-35-10](https://github.com/user-attachments/assets/c02adfec-5036-425c-b35b-88e4ed6836ea)
Can someone help me understand what is going on? And maybe how to correct this behavior.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUWVtv67aW_jXMy4IMirRl6yEPdpxMA7SDQbOn82hQ0rLNbokUSCrZ6a8fLEryLU72Pm160BMEsiRyXfmtCynlvd4ZxFs2W7HZ-kZ1YW_d7bJ-Vs7eP_xig7opbPV6-xPWtWXiDh5BNRDcqzY7CBY6jxD2CL92PkBpm1bX6GhghwadCggKlPfYFPUrlLZCGnOdAWsiHZutyrarVbCOzdZMLPYhtJ7JJRMPTDyMYxOefvtjYjDQa_ngXz2Ta_csRVJh6m3JRA4ve13uYa-eSWalfVurV2hU22IFKgDLOP-23QrOueAs4xP4ssdeJe2jLr5RdY0-QGu910WNE3hkYt7A72Tc0WZPIojgWdUdEuOUGAITCwVOmco2_RBppSI7HbQ1b1SAlz06hEcolYHSmuBsDQoabfRowITxNePL4ZrxRmkzcZ5l_PBq-O8fhWQiZbOVsRsfKnLpm_fE4jgQr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