The cg_clif
project has made significant progress in compiling Rust to C, achieving a 95.9% pass rate on the Rust test suite. This compiler leverages Cranelift as a backend and utilizes a custom ABI for passing Rust data structures. Notably, it's now functional on more unusual platforms like wasm32-wasi
and thumbv6m-none-eabi
(for embedded ARM devices). While performance isn't a primary focus currently, basic functionality and compatibility are progressing rapidly, demonstrating the potential for compiling Rust to a portable C representation.
This blog post by Fractal Fir details the ongoing development of cg_clif
(now clif-util
), a tool designed to compile Rust code into C code. The author focuses on recent progress and challenges encountered while targeting "odd platforms"—specifically, WebAssembly (Wasm) and embedded systems like the AVR microcontroller family.
A significant milestone is reaching a 95.9% pass rate on the Rust compiler's extensive test suite when compiling to C and subsequently to Wasm. This achievement highlights the project's increasing maturity and ability to handle complex Rust constructs even when targeting non-traditional environments. The author attributes this success partly to the use of Cranelift, a code generation library that facilitates targeting diverse architectures.
However, the journey isn't without hurdles. The post explains that supporting inline assembly, a feature frequently used for low-level optimization and hardware interaction, presents significant difficulties. The disparity between the assembly syntax understood by the Rust compiler's LLVM backend and the syntax expected by the Wasm target requires intricate translation, a problem not yet fully solved. The author acknowledges this as a major area of ongoing work.
Furthermore, the post discusses the challenges in targeting the AVR microcontroller architecture. AVR, a popular choice for resource-constrained embedded systems, poses unique constraints due to its limited instruction set and memory capacity. The author describes working on implementing calling conventions compatible with AVR and tackling the intricacies of handling data types and memory management specific to this platform. While significant progress has been made, targeting AVR remains a work in progress, with complete support still on the horizon.
The overarching goal of cg_clif
is to expand the reach of Rust code by enabling compilation to C, thereby unlocking the ability to target platforms not directly supported by the standard Rust compiler. The project leverages the Cranelift code generation library and the clif
intermediate representation to achieve this cross-compilation. While challenges remain, particularly regarding inline assembly and support for resource-constrained environments like AVR, the project demonstrates promising progress towards enabling broader platform compatibility for Rust code. The author expresses optimism about future developments and invites contributions from the community.
Summary of Comments ( 164 )
https://news.ycombinator.com/item?id=43661329
Hacker News users discussed the impressive 95.9% test pass rate of the Rust-to-C compiler, particularly its ability to target unusual platforms like the Sega Saturn and Sony PlayStation. Some expressed skepticism about the practical applications, questioning the performance implications and debugging challenges of such a complex transpilation process. Others highlighted the potential benefits for code reuse and portability, enabling Rust code to run on legacy or resource-constrained systems. The project's novelty and ambition were generally praised, with several commenters expressing interest in the developer's approach and future developments. Some also debated the suitability of "compiler" versus "transpiler" to describe the project. There was also discussion around specific technical aspects, like memory management and the handling of Rust's borrow checker within the C output.
The Hacker News post titled "Rust to C compiler – 95.9% test pass rate, odd platforms" sparked a discussion with several interesting comments. Many commenters focused on the complexities and nuances of compiling Rust to C, particularly given Rust's unique memory management features.
One commenter highlighted the challenges inherent in translating Rust's borrow checker and ownership model into C, which lacks these built-in mechanisms. They questioned how the compiler handled these crucial aspects of Rust, expressing skepticism about achieving true compatibility without significant runtime overhead or limitations. This comment resonated with others who also expressed concern about the potential performance implications and the difficulty of replicating Rust's safety guarantees in C.
Another commenter pointed out the inherent difficulty in targeting "odd platforms," as mentioned in the title. They elaborated on the potential issues with varying C standard library implementations and the complexities of ensuring compatibility across diverse architectures and operating systems. This prompted a discussion about the trade-offs between portability and performance when attempting such a compilation process.
Several comments also touched on the potential use cases of such a compiler. Some suggested it could be valuable for embedded systems or environments where Rust isn't directly supported. Others questioned the practicality, arguing that if the target platform supports a C compiler, it might also be feasible to support a Rust compiler directly, potentially negating the need for a transpilation step.
The discussion also explored alternative approaches, such as compiling Rust to LLVM bitcode and then using LLVM to generate C code. This was presented as a potentially more robust approach that could leverage LLVM's optimizations and platform support.
Finally, some comments expressed interest in the specific platforms targeted by the project and requested more details about the remaining 4.1% of failing tests. They were curious about the nature of these failures and whether they represented fundamental limitations or solvable issues. Overall, the comments reflected a mixture of curiosity, skepticism, and cautious optimism about the potential of a Rust-to-C compiler.