Rust for P2: Request for help
I started looking into getting Rust working on P2. I don't actually know the language, so it's a bit of an uphill battle for me. However, I made some progress, described below. I'm looking for someone who knows the language and can actually take on the bulk of the effort with rust to get it working. My main hiccup is not fully yet knowing how all it's support tools work (cargo, etc) and there seem to be conflicts. The theory is that it should be fairly simple--once rust can generate LLVM-IR compatible with P2's data layout, the llvm backend should be able to handle the rest.
What I've gotten working so far:
1. Created a fork of rust, set the llvm submodule to track my LLVM, and cherry-picked in a couple of things from rust's LLVM fork to get things compatible. See here: https://github.com/ne75/rust
2. Modified the bare minimum for rust to recognize the P2 target in LLVM as a target triple, instantiate the target machine, etc.
3. Successfully compiled rustc for Apple M1.
4. Wrote a p2.json to describe P2 to the rustc front-end, and can execute rustc with p2 as the final target without complaints from rustc.
Now for the fun parts. I tried compiling a simple "no_std" program to no luck (https://docs.rust-embedded.org/embedonomicon/smallest-no-std.html). It fails because there's no core library for rust available. So, going back to try to compile that, it fails on the higher-level front end not having an understanding of the ABIs available. I'm hoping someone who has a better understanding of how rustc and the front end works with LLVM can take a look and see if it's a matter of just copying what AVR or MSP430 did.
How to get everything compiling:
basically, follow https://rustc-dev-guide.rust-lang.org/building/how-to-build-and-run.html, I've described specifically below.
1. clone the repo
2. run ./x.py setup
3. update the config.toml (attached). If you are building on something other than apple M1, update the targets
line to build LLVM for whatever the host is. This is necessary because the build process will compile LLVM for the host, use that compilation to compile rustc in stages, and then also use that LLVM for all targets the final rustc will make available.
4. run ./x.py build
5. run ./x.py install
rustc will be installed to /opt/p2rust
. Change in config.toml if different location is necessary
6. run `./x.py build library/core --target p2.json
The last one is that fails. Anyone brave enough to take a stab at this? I think the core is all there, now it's just about finishing up the plumbing I think.
Comments
I'm probably as confused as you that there has been no response thus far.
Rust seems to not have caught on the way I expected.
I actually made some descent progress today. I got past the first ABI issue, on to the next issue, where returning tuples is causing problems with the p2 llvm target. I might actually be able to make the core compiler work with fairly little effort
I was curious about rust, kind of heard about it, not really in the loop. I didn't even know about GO.
From all the documentation it looks like it just builds programs that are compile with C++ compiler under the covers. So everything you have is there you just need to rewrite the builder rust that creates the programs to be compiled.
Mike
The compiler itself is written in rust as well, it’s completely it’s own language and compiler, but it compiles down to LLVM IR first and uses LLVM as the codegen backend, which is why I’m hoping adding P2 as a target is relatively straight forward. I actually got the core library compiling, so the target has been added successfully, I just need to figure out the builtins library, which is giving me some problems, and then get the linker properly setup.
Hey, sorry, I just saw this. I'd love to help out. I'll take a look tonight or later this week.
@DavidZemon I made even more progress last night, libcore compiles, I'm working on creating a crate for the builtins library, but I'm super close to having a compilable example, I think. Then, I just need to instantiate the inline asm functionality for this target and I'll be able to do a blink example. From there, it'll be out of my hands and you can take over I'm thinking the way we can split up work is for things that involve LLVM and the backend stuff I can handle, for the library/rust code side you can do?
I guess it's finally time for me to order a new P2 board. The only one I have is the original P2 eval board (rev A).
That breakdown sounds great, btw.
Got a blinking LED with rust! Will create a separate post with details and next steps.
Fantastic breakthrough! Congrats!