www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Quick and dirty support for RISC-V

reply e-y-e <yurtqbqn grr.la> writes:
In a previous post a few days ago, I asked what the easiest way 
of getting D code running on RISC-V would be. I thought the 
process might take quite a while and more knowledge of compilers, 
hardware etc than I currently have.

But technically, D code can now be run on RISC-V. Sort of.

I followed Nicholas Wilson's direction to get the RISC-V 
toolchain and LLVM built and working. This was the hardest part 
for me, about 5 seperate riscv-* items needed to be compiled/run 
correctly in order to build and run a simple helloworld.c program 
in an ISA simulator. This toolchain was used to build LDC (using 
the cmake flag -DLLVM_CONFIG=<path-to-riscv>/bin/llvm-config).

To start off, druntime wouldn't build because it had no idea 
about RISC-V yet, but it wasn't a problem as runtime-less D is 
probably par for the course on the eventual architecture. 'make 
ldc2' worked fine so that was that.

I have no experience with compilers, so I had no clue where to 
begin in order to get helloworld.d running. Then I remembered 
Adam D Ruppe's opening advice from dconf 2014 talk, and decided 
to just try it and see if it works. Naturally the first time it 
didn't, but by silencing the very short trail of compile errors, 
and playing around with the build method & command arguments a 
little, I eventually got it to work! Mainly due to LLVM being an 
amazing piece of software but that's for another time.

Obviously this approach has a few drawbacks, but I can run D on a 
RISC-V ISA, which was my goal.

Drawbacks:

- Only tested on one toolchain built for one ISA (RV32IMAC)
- Must be betterC code, and also no correct consideration for 
OS-dependent items (such as errno)
- The process of compiling something is a little overwhelming, 
and undocumented for now
- The repo doesn't really follow a proper workflow
- And probably many more, very little is tested!

This was not meant to be a project for anyone other than myself, 
I just wanted to get something working. But now that it is, I 
will start the process of documenting how to compile things, 
migrating the source from gitlab to github and anything else that 
needs to be done.

For now though, these are the commands I used:

To build compiler:

git clone --recurse-submodules 
https://github.com/e-y-e/riscv-ldc.git
cd riscv-ldc
mkdir build && cd build
cmake .. -DLLVM_CONFIG=<path-to-riscv>/bin/llvm-config
make -jN ldc2

To compile:

<path-to-riscv-ldc>/build/bin/ldc2 -march=riscv -betterC 
-defaultlib= -output-ll helloworld.d
<path-to-riscv>/bin/llc helloworld.ll
<path-to-riscv>/bin/riscv32-unknown-elf-gcc -o helloworld 
helloworld.s

To run using riscv-isa-simulator:

<path-to-riscv>/bin/spike --isa=RV32IMAC 
<path-to-riscv>/riscv32-unknown-elf/bin/pk helloworld

Process requires:
https://github.com/riscv/riscv-gnu-toolchain
https://github.com/riscv/riscv-llvm
https://github.com/riscv/riscv-pk
https://github.com/riscv/riscv-fesvr
https://github.com/riscv/riscv-spike

Work is up at the following links so you can see how little 
modification was required:

- LDC: https://gitlab.com/e-y-e/riscv-ldc
- DRuntime: https://gitlab.com/e-y-e/riscv-druntime
- Phobos: https://gitlab.com/e-y-e/riscv-phobos

---

TL;DR: Got D running on RV32IMAC ISA Simulator with minimal 
modification. Wanted to share because it made me happy that it 
worked.
Jan 04 2017
parent reply Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Wednesday, 4 January 2017 at 17:12:54 UTC, e-y-e wrote:
 In a previous post a few days ago, I asked what the easiest way 
 of getting D code running on RISC-V would be. I thought the 
 process might take quite a while and more knowledge of 
 compilers, hardware etc than I currently have.

 [...]
Excellent! You said you did some hacking on LLVM so small LDC checklist: Have you set all the correct predefined versions? ABI? If you have any questions please hop on to our gitter: https://gitter.im/ldc-developers/main
Jan 04 2017
parent e-y-e <yurtqbqn grr.la> writes:
On Thursday, 5 January 2017 at 00:46:11 UTC, Nicholas Wilson 
wrote:
 On Wednesday, 4 January 2017 at 17:12:54 UTC, e-y-e wrote:
 [...]
Excellent! You said you did some hacking on LLVM so small LDC checklist:
On LDC only
 Have you set all the correct predefined versions?
I have defined RISCV and RISCV64, but I have some questions on how to go further than this.
 ABI?
Another thing I have to look into, I only changed the things necessary to make it work.
 If you have any questions please hop on to our gitter: 
 https://gitter.im/ldc-developers/main
Will do!
Jan 05 2017