Hi.
I'm exploring Nim for a bare metal project in a RISCV64 environment. I seem to be hitting a compiler codegen problem and could do with some advice.
My setup:
$ nim --version
Nim Compiler Version 1.6.12 [Linux: amd64]
Compiled at 2023-03-10
Copyright (c) 2006-2023 by Andreas Rumpf
git hash: 1aa9273640c0c51486cf3a7b67282fe58f360e91
active boot switches: -d:release
My code:
import std/volatile
when isMainModule:
const uart_ptr = cast[ptr char](0x1000_0000)
volatile_store(uart_ptr, 'H')
while true:
discard
I build it roughly so:
nim c -d:release --gc:none --cpu:riscv64 --os:standalone --passC:-Os --passC:-mcmodel=medany --passL:'-Wl,-T,linkscript.ld' --passL:-nostdlib --passL:boot.o --passL:-mcmodel=medany main.nim
The error:
nim c -d:release --gc:none --cpu:riscv64 --os:standalone --passC:-Os --passC:-mcmodel=medany --passL:'-Wl,-T,linkscript.ld' --passL:-nostdlib --passL:boot.o --passL:-mcmodel=medany main.nim
Hint: used config file '/home/robin/.choosenim/toolchains/nim-1.6.12/config/nim.cfg' [Conf]
Hint: used config file '/home/robin/.choosenim/toolchains/nim-1.6.12/config/config.nims' [Conf]
Hint: used config file '/home/robin/Work/experiments/nim/nimkernel/exp/attempt.0/nim.cfg' [Conf]
..........................................
CC: main.nim
/home/robin/.cache/nim/main_r/@mmain.nim.c:24:4: error: expected identifier or '(' before 'NIM_CHAR'
24 | *((NIM_CHAR volatile*)(((NIM_CHAR*) 268435456))) = 72;
| ^~~~~~~~
/home/robin/.cache/nim/main_r/@mmain.nim.c:24:23: error: expected ')' before '(' token
24 | *((NIM_CHAR volatile*)(((NIM_CHAR*) 268435456))) = 72;
| ^
| )
Error: execution of an external compiler program '/home/robin/Work/toolchains/x-tools/riscv64-unknown-elf/bin/riscv64-unknown-elf-gcc -c -w -fmax-errors=3 -Os -mcmodel=medany -O3 -fno-strict-aliasing -fno-ident -I/home/robin/.choosenim/toolchains/nim-1.6.12/lib -I/home/robin/Work/experiments/nim/nimkernel/exp/attempt.0 -o /home/robin/.cache/nim/main_r/@mmain.nim.c.o /home/robin/.cache/nim/main_r/@mmain.nim.c' failed with exit code: 1
make: *** [Makefile:5: main] Error 1
I think this is the same issue as: https://github.com/nim-lang/Nim/issues/14623 but am not entirely sure.
Any pointers appreciated (pun not intended!).
Cheers
Instead of putting everything at the top level, wrap it into a proc and then invoke the proc within the when isMainModule block:
import std/volatile
proc loop =
const uart_ptr = cast[ptr char](0x1000_0000)
volatile_store(uart_ptr, 'H')
while true:
discard
when isMainModule:
loop()
It seems like it is related to that issue, but that's the only way I could make it work